package com.ruoyi.tc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.tc.entity.AssetOfficialAccount;
import com.ruoyi.tc.entity.AssetTask;
import com.ruoyi.tc.entity.Unit;
import com.ruoyi.tc.entity.po.AssetMiniProgramsCpPo;
import com.ruoyi.tc.entity.po.AssetOfficialAccountCpPo;
import com.ruoyi.tc.entity.request.AssetOfficialAccountPageRequest;
import com.ruoyi.tc.mapper.AssetOfficialAccountMapper;
import com.ruoyi.tc.service.AssetOfficialAccountCpService;
import com.ruoyi.tc.service.AssetOfficialAccountService;
import com.ruoyi.tc.service.AssetTaskService;
import com.ruoyi.tc.service.UnitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 公众号资产(asset_official_account)表服务实现类
 *
 * @author makejava
 * @since 2024-11-28 17:13:07
 */
@Service("assetOfficialAccountService")
public class AssetOfficialAccountServiceImpl extends ServiceImpl<AssetOfficialAccountMapper, AssetOfficialAccount> implements AssetOfficialAccountService {
    @Resource
    private AssetTaskService assetTaskService;

    @Resource
    private UnitService unitService;

    @Resource
    private AssetOfficialAccountCpService assetOfficialAccountCpService;

    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param as   查询实体
     * @return 所有数据
     */
    @Override
    public Page<AssetOfficialAccount> page(Page<AssetOfficialAccount> page, AssetOfficialAccountPageRequest as) {
        return baseMapper.page(page, as);
    }

    /**
     * 逻辑删除
     */
    @Override
    public void deleteById(Long id) {
        baseMapper.deleteById(id);
        QueryWrapper<AssetOfficialAccountCpPo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("asset_id", id);
        assetOfficialAccountCpService.remove(queryWrapper);
    }


    /**
     * 查询所有数据
     *
     * @param as 查询实体
     * @return 所有数据
     */
    @Override
    public List<AssetOfficialAccount> getAllList(AssetOfficialAccountPageRequest as) {
        return baseMapper.page(as);
    }


    @Override
    public void deleteIdList(List<Long> idList) {
        baseMapper.deleteIdList(idList);
    }

    /**
     * 新增公众号数据
     *
     * @param assetOfficialAccount 实体
     * @return 新增结果
     */
    @Override
    public Boolean add(AssetOfficialAccount assetOfficialAccount) {
//        List<Unit> list3 = unitService.lambdaQuery()
//                .eq(Unit::getNickName, assetOfficialAccount.getSsdw()).eq(Unit::getDelFlag, 0).list();
//        if (list3 == null || list3.isEmpty()) {
//            throw new ServiceException("请选择已有单位!");
//        }

        List<AssetOfficialAccount> list1 = lambdaQuery()
                .eq(AssetOfficialAccount::getSsdw, assetOfficialAccount.getSsdw())
                .eq(AssetOfficialAccount::getGzhmc, assetOfficialAccount.getGzhmc()).list();
        if (list1 != null&& !list1.isEmpty()){
            throw new ServiceException("不允许新增重复资产!");
        }

        if (assetOfficialAccount.getCdList() != null && !assetOfficialAccount.getCdList().isEmpty()) {
            StringBuilder a = new StringBuilder();
            StringBuilder b = new StringBuilder();
            StringBuilder c = new StringBuilder();
            assetOfficialAccount.getCdList().forEach(x -> {
                if(!Objects.equals(x.getCdmc(), "") &&x.getCdmc()!=null){
                    a.append(x.getCdmc()).append("|");
                }
                if(!Objects.equals(x.getCdlj(), "") &&x.getCdlj()!=null){
                    b.append(x.getCdlj()).append("|");
                }
                if(!Objects.equals(x.getCdlx(), "") &&x.getCdlx()!=null){
                    c.append(x.getCdlx()).append("|");
                }
            });
            assetOfficialAccount.setCdmc(a.toString());
            assetOfficialAccount.setCdlj(b.toString());
            assetOfficialAccount.setCdlx(c.toString());
        }


        List<Unit> list = unitService.lambdaQuery().eq(Unit::getNickName, assetOfficialAccount.getSsdw())
                .eq(Unit::getDelFlag, 0).list();
        if (list == null || list.isEmpty()) {
            throw new ServiceException("请选择已有单位!");
        }
        save(assetOfficialAccount);
        AssetOfficialAccountCpPo a = new AssetOfficialAccountCpPo();
        BeanUtil.copyProperties(assetOfficialAccount, a);
        Integer i = unitService.selectTaskId(assetOfficialAccount.getSsdw(), "2");
        if (i != null) {
            a.setTaskId(i);
            a.setAssetId(assetOfficialAccount.getId());
            assetOfficialAccountCpService.save(a);
        }
        return true;
    }

    /**
     * 编辑公众号数据
     *
     * @param assetOfficialAccount 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetOfficialAccount assetOfficialAccount) {
        List<AssetOfficialAccount> list4= lambdaQuery()
                .eq(AssetOfficialAccount::getSsdw, assetOfficialAccount.getSsdw())
                .eq(AssetOfficialAccount::getGzhmc, assetOfficialAccount.getGzhmc()).list();
        if (list4 != null&& !list4.isEmpty()){
            throw new ServiceException("不允许新增重复资产!");
        }
        if (assetOfficialAccount.getCdList() != null && !assetOfficialAccount.getCdList().isEmpty()) {
            StringBuilder a = new StringBuilder();
            StringBuilder b = new StringBuilder();
            StringBuilder c = new StringBuilder();
            assetOfficialAccount.getCdList().forEach(x -> {
                if(!Objects.equals(x.getCdmc(), "") &&x.getCdmc()!=null){
                    a.append(x.getCdmc()).append("|");
                }
                if(!Objects.equals(x.getCdlj(), "") &&x.getCdlj()!=null){
                    b.append(x.getCdlj()).append("|");
                }
                if(!Objects.equals(x.getCdlx(), "") &&x.getCdlx()!=null){
                    c.append(x.getCdlx()).append("|");
                }
            });
            assetOfficialAccount.setCdmc(a.toString());
            assetOfficialAccount.setCdlj(b.toString());
            assetOfficialAccount.setCdlx(c.toString());
        }


        List<Unit> list1 = unitService.lambdaQuery().eq(Unit::getNickName, assetOfficialAccount.getSsdw()).eq(Unit::getDelFlag, 0).list();
        if (list1 == null || list1.isEmpty()) {
            throw new ServiceException("请选择已有单位!");
        }

        updateById(assetOfficialAccount);
        List<AssetOfficialAccountCpPo> list = assetOfficialAccountCpService.lambdaQuery()
                .eq(AssetOfficialAccountCpPo::getAssetId, assetOfficialAccount.getId()).list();
        list.forEach(x -> {
            Integer status = x.getStatus();
            Integer taskId = x.getTaskId();
            Long appId = x.getAccountId();
            String auditState = x.getAuditState();
            String auditYy = x.getAuditYy();
            BeanUtil.copyProperties(assetOfficialAccount, x);
            x.setTaskId(taskId);
            x.setAssetId(assetOfficialAccount.getId());
            x.setStatus(status);
            x.setAccountId(appId);
            x.setAuditYy(auditYy);
            x.setAuditState(auditState);
            assetOfficialAccountCpService.updateById(x);
        });

        //根据修改的单位名称查询任务表中是否有此单位的任务，如果有修改资产中的任务id，如果没有删除
        AssetTask assetTask = assetTaskService.findByDwmc(assetOfficialAccount.getGzhmc());
        if (assetTask != null) {
            assetOfficialAccountCpService.deletByAssetId(assetOfficialAccount.getId());
        } else {
            //根据资产id查询旧资产
            AssetOfficialAccountCpPo assetOfficialAccountCpPo = assetOfficialAccountCpService.findDwmc(assetOfficialAccount.getId());
            if (assetOfficialAccountCpPo!=null){
                assetOfficialAccountCpPo.setGzhmc(assetOfficialAccount.getGzhmc());
                assetOfficialAccountCpPo.setTaskId(assetTask.getId());
                //修改
                assetOfficialAccountCpService.updateById(assetOfficialAccountCpPo);
            }

        }
        return true;
    }


    @Override
    public Integer findBydwmc(String part) {
        return baseMapper.findBydwmc(part);
    }
}
