package com.zjrs.zwnw.dbgl.blo.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.mohrss.leaf.core.framework.domain.blo.impl.BLOImpl;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.persistens.PageUtil;
import org.mohrss.leaf.core.framework.util.CurrentUser;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.dbgl.blo.DbrwclPLO;
import com.zjrs.zwnw.dbgl.dto.*;
import com.zjrs.zwnw.dbgl.entity.*;
import com.zjrs.zwnw.dbgl.po.RsdbgldbxmPO;
import com.zjrs.zwnw.dbgl.po.RsdbgldbxmgzqdPO;
import com.zjrs.zwnw.dbgl.po.RsdbgldbxmgzqdlogPO;
import com.zjrs.zwnw.dbgl.po.RsdbglgzqdPO;
import com.zjrs.zwnw.portal.blo.CommonBLO;
import com.zjrs.zwnw.portal.entity.VSSgOrgMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @Author chenzhibiao
 * @create 2022/6/30 8:43
 */
@Service
public class DbrwclimplPLO extends BLOImpl implements DbrwclPLO {
    @Resource
    private DbrwMapper dbrwMapper;
    @Resource
    private CommonBLO commonBLO;

    @Resource
    private VSSgOrgMapper vSSgOrgMapper;
    @Resource
    private DbxmMapper dbxmMapper;
    @Resource
    private DbxmGzqdMapper dbxmGzqdMapper;
    @Resource
    private GzqdMapper gzqdMapper;
    @Resource
    private DbxmGzqdLogMapper dbxmGzqdLogMapper;




    @Override
    public PageResult getGzqdffList(DbrwglDbrwclQueryDTO dto, Integer pagenum, Integer pagesize){
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
//        userid ="1c872fa8-8681-42b4-a221-04f5a7759b74";
//        orgid = "564c3dea-b00d-47af-acd6-794baa41e688";
        dto.setUserid(userid);
        dto.setOrgid(orgid);
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) dbrwMapper.getGzqdffList(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
    @Override
    public PageResult getDbrwListByXmid(DbrwglCommQueryDTO dto, Integer pagenum, Integer pagesize){
        Page<Map<String, Object>> page = new Page<>(pagenum, pagesize);
        Page<Map<String, Object>> pageResult = (Page) dbrwMapper.getDbrwListByXmid(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
    @Override
    public PageResult getGzqdListByXmid(DbrwglCommQueryDTO dto, Integer pagenum, Integer pagesize){
        CurrentUser currentUser = getCurrentUser();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        dto.setSsbm(orgid);
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
//        userid = "1c872fa8-8681-42b4-a221-04f5a7759b74";
        dto.setUserid(userid);
        Page<Map<String, Object>> page = new Page<>(pagenum, pagesize);
        Page<Map<String, Object>> pageResult = (Page) dbrwMapper.getGzqdListByXmid(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void updateFfgzdq(DbrwglCommDTO dto){
        RsdbgldbxmPO po = new RsdbgldbxmPO();

        //校验是否选择了工作清单，提示：没有待分配的工作清单，请先选择工作清单。
        QueryWrapper<RsdbgldbxmgzqdPO> queryWrapper = new QueryWrapper<RsdbgldbxmgzqdPO>();
        int cnt = this.dbxmGzqdMapper.selectCount(queryWrapper);
        if(cnt == 0 ){
            throw new AppException("没有待分配的工作清单，请先选择工作清单。");
        }

        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        String currentTime = this.commonBLO.getCurrenttime();
        po.setBae004(userid);//最近修改人

        po.setRsdbgldbxmlsh(dto.getIds());
        po.setFpzt("1");
        this.dbxmMapper.updateById(po);
    }
    @Override
    public void delGzqd(DbrwglCommDTO dto){
        this.dbxmGzqdMapper.deleteById(dto.getIds());
    }

    @Override
    public void updateGzqdXz(DbrwglDbrwclGzqdxzDTO dto){
        RsdbgldbxmgzqdPO po = new RsdbgldbxmgzqdPO();
        BeanUtils.copyProperties(dto,po);

        //关联表赋值责任人、责任部门
        RsdbglgzqdPO rsdbglgzqdPO = this.gzqdMapper.selectById(dto.getRsdbglgzqdlsh());
        if(rsdbglgzqdPO != null){
            po.setZrbm(rsdbglgzqdPO.getZrbm());
            po.setZrr(rsdbglgzqdPO.getZrr());
        }

        this.dbxmGzqdMapper.insert(po);
    }

    @Override
    public PageResult getGzjzshList(DbrwglDbrwclGzjzshQueryDTO dto, Integer pagenum, Integer pagesize){
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        //userid = "1c872fa8-8681-42b4-a221-04f5a7759b74";
        dto.setUserid(userid);
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) dbrwMapper.getGzjzshList(page,dto);
        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public void updateGzqdshBatch(DbrwglGzqdBatchShDTO dto){
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        String currentTime = this.commonBLO.getCurrenttime();

        String ids = dto.getIds();
        String[] idsArr = ids.split(",");
        List<String> list = Arrays.asList(idsArr);

        for(String qdid : list){
            //处理清单表
            RsdbglgzqdPO po = new RsdbglgzqdPO();
            po.setRsdbglgzqdlsh(qdid);
            po.setSdpj(dto.getSdpj());
            po.setZlpj(dto.getZlpj());
            this.gzqdMapper.updateById(po);

            //处理项目清单关联表
            List<RsdbgldbxmgzqdPO> dbxmgzqdPOList = this.dbxmGzqdMapper.getDbxmGzqdByqdidList(qdid);
            RsdbglgzqdPO qdPO = new RsdbglgzqdPO();
            qdPO = this.gzqdMapper.selectById(qdid);
            for(RsdbgldbxmgzqdPO dbxmgzqdPO:dbxmgzqdPOList){
                dbxmgzqdPO.setGzqd(qdPO.getGzqd());//工作清单
                dbxmgzqdPO.setShjg(orgid);//审核机构
                dbxmgzqdPO.setShsj(currentTime);//审核时间
                this.dbxmGzqdMapper.updateById(dbxmgzqdPO);
                //处理审核历史表
                RsdbgldbxmgzqdlogPO logPO = new RsdbgldbxmgzqdlogPO();
                BeanUtils.copyProperties(dbxmgzqdPO,logPO);
                logPO.setBae003(null);
                this.dbxmGzqdLogMapper.insert(logPO);
            }
        }
    }

    @Override
    public void updateGzqdsh(DbrwglGzqdShDTO dto){
        CurrentUser currentUser = getCurrentUser();
        String userid = currentUser.getUserid()==null?"":currentUser.getUserid().trim();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        String currentTime = this.commonBLO.getCurrenttime();
        //处理清单表
        RsdbglgzqdPO po = new RsdbglgzqdPO();
        BeanUtils.copyProperties(dto,po);
        po.setBae004(userid);//最近修改人
        po.setShjg(orgid);
        po.setShsj(currentTime);
        this.gzqdMapper.updateById(po);

        //处理项目清单关联表
        List<RsdbgldbxmgzqdPO> dbxmgzqdPOList = this.dbxmGzqdMapper.getDbxmGzqdByqdidList(dto.getRsdbglgzqdlsh());
        for(RsdbgldbxmgzqdPO dbxmgzqdPO:dbxmgzqdPOList){
            dbxmgzqdPO.setGzqd(dto.getGzqd());//工作清单
            dbxmgzqdPO.setShsj(currentTime);//审核时间
            this.dbxmGzqdMapper.updateById(dbxmgzqdPO);
            //处理审核历史表
            RsdbgldbxmgzqdlogPO logPO = new RsdbgldbxmgzqdlogPO();
            BeanUtils.copyProperties(dbxmgzqdPO,logPO);
            logPO.setBae003(null);
            this.dbxmGzqdLogMapper.insert(logPO);
        }

    }
}
