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.AssetMiniPrograms;
import com.ruoyi.tc.entity.Unit;
import com.ruoyi.tc.entity.po.AssetMiniProgramsCpPo;
import com.ruoyi.tc.entity.request.AssetMiniProgramsPageRequest;
import com.ruoyi.tc.entity.zztb.AssetMiniProgramsZztbPo;
import com.ruoyi.tc.mapper.AssetMiniProgramsMapper;
import com.ruoyi.tc.service.AssetMiniProgramsCpService;
import com.ruoyi.tc.service.AssetMiniProgramsService;
import com.ruoyi.tc.service.AssetTaskService;
import com.ruoyi.tc.service.UnitService;
import com.ruoyi.tc.zztbService.AssetMiniProgramsZztbService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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

/**
 * 小程序资产(asset_mini_programs)表服务实现类
 *
 * @author makejava
 * @since 2024-11-28 17:13:07
 */
@Service("assetMiniProgramsService")
public class AssetMiniProgramsServiceImpl extends ServiceImpl<AssetMiniProgramsMapper, AssetMiniPrograms> implements AssetMiniProgramsService {
    @Resource
    private AssetTaskService assetTaskService;
    @Resource
    private AssetMiniProgramsCpService assetMiniProgramsCpService;

    @Resource
    private UnitService unitService;

    @Resource
    private AssetMiniProgramsZztbService assetMiniProgramsZztbService;

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

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


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

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

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

        List<AssetMiniPrograms> list1 = lambdaQuery().eq(AssetMiniPrograms::getSsdw, assetMiniPrograms.getSsdw())
                .eq(AssetMiniPrograms::getXcxmc, assetMiniPrograms.getXcxmc()).list();
        if (list1 != null && !list1.isEmpty()) {
            throw new ServiceException("不允许新增重复资产!");
        }
        if (assetMiniPrograms.getXzType() == 1) {
            //单位端新增
            AssetMiniProgramsZztbPo assetMiniProgramsZztbPo = new AssetMiniProgramsZztbPo();
            BeanUtils.copyProperties(assetMiniPrograms, assetMiniProgramsZztbPo);
            assetMiniProgramsZztbPo.setAuditState("1");
            assetMiniProgramsZztbPo.setAuditYy(null);
            assetMiniProgramsZztbService.save(assetMiniProgramsZztbPo);
        } else {
            //管理端新增
            save(assetMiniPrograms);
            AssetMiniProgramsCpPo assetMiniProgramsCpPo = new AssetMiniProgramsCpPo();
            BeanUtil.copyProperties(assetMiniPrograms, assetMiniProgramsCpPo);
            Integer i = unitService.selectTaskId(assetMiniPrograms.getSsdw(), "1");
            if (i != null) {
                assetMiniProgramsCpPo.setTaskId(i);
                assetMiniProgramsCpPo.setXzType(1);
                assetMiniProgramsCpPo.setAssetId(assetMiniPrograms.getId());
                assetMiniProgramsCpService.save(assetMiniProgramsCpPo);
            }
        }

        return true;
    }

    /**
     * 编辑小程序数据
     *
     * @param assetMiniPrograms 实体
     * @return 编辑结果
     */
    @Override
    public Boolean edit(AssetMiniPrograms assetMiniPrograms) {

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


        updateById(assetMiniPrograms);
        List<AssetMiniProgramsCpPo> list = assetMiniProgramsCpService.lambdaQuery()
                .eq(AssetMiniProgramsCpPo::getAssetId, assetMiniPrograms.getId()).list();
        list.forEach(x -> {
            Integer status = x.getStatus();
            Integer taskId = x.getTaskId();
            Long miniId = x.getMiniId();
            String auditState = x.getAuditState();
            String auditYy = x.getAuditYy();
            BeanUtil.copyProperties(assetMiniPrograms, x);
            x.setTaskId(taskId);
            x.setAssetId(assetMiniPrograms.getId());
            x.setStatus(status);
            x.setMiniId(miniId);
            x.setAuditYy(auditYy);
            x.setAuditState(auditState);
            assetMiniProgramsCpService.updateById(x);
        });
        //根据修改的单位名称查询任务表中是否有此单位的任务，如果有修改资产中的任务id，如果没有删除
//        AssetTask assetTask = assetTaskService.findByDwmc(assetMiniPrograms.getXcxmc());
//        if (assetTask == null) {
//            assetMiniProgramsCpService.deletByAssetId(assetMiniPrograms.getId());
//        } else {
//            //根据资产id查询旧资产
//            AssetMiniProgramsCpPo assetMiniProgramsCpPo = assetMiniProgramsCpService.findDwmc(assetMiniPrograms.getId());
//            if (assetMiniProgramsCpPo!=null){
//                assetMiniProgramsCpPo.setXcxmc(assetMiniPrograms.getXcxmc());
//                assetMiniProgramsCpPo.setTaskId(assetTask.getId());
//                assetMiniProgramsCpService.updateById(assetMiniProgramsCpPo);
//            }
//
//            //修改
//
//        }
        return true;
    }


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