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.apache.commons.lang.StringUtils;
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.DbrwglCommPLO;
import com.zjrs.zwnw.dbgl.blo.DbrwglPLO;
import com.zjrs.zwnw.dbgl.dto.*;
import com.zjrs.zwnw.dbgl.entity.DbrwMapper;
import com.zjrs.zwnw.dbgl.entity.DbrwglCommMapper;
import com.zjrs.zwnw.dbgl.entity.DbxmMapper;
import com.zjrs.zwnw.dbgl.entity.DbxmZrbmMapper;
import com.zjrs.zwnw.dbgl.po.RsdbgldbrwPO;
import com.zjrs.zwnw.dbgl.po.RsdbgldbxmPO;
import com.zjrs.zwnw.dbgl.po.RsdbgldbxmzrbmPO;
import com.zjrs.zwnw.portal.blo.CommonBLO;
import com.zjrs.zwnw.portal.entity.VSSgOrgMapper;
import com.zjrs.zwnw.portal.po.VSSgOrgPO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

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


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

    @Resource
    private VSSgOrgMapper vSSgOrgMapper;
    @Resource
    private DbxmMapper dbxmMapper;
    @Resource
    private DbxmZrbmMapper dbxmZrbmMapper;
    @Resource
    private DbrwglCommMapper dbrwglCommMapper;
    @Resource
    private DbrwglCommPLO dbrwglCommPLO;



    @Override
    public PageResult getDbrwList(DbrwglDbrwQueryDTO dto, Integer pagenum, Integer pagesize){
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        Page<Map<String, Object>> pageResult = (Page) dbrwMapper.getDbrwList(page,dto);

        //处理分管领导字段
        List<Map<String, Object>> list = pageResult.getRecords();

        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }
    @Override
    public String saveDbrw(DbrwglDbrwUDTO dto){
        String id = dto.getRsdbgldbrwlsh();
        RsdbgldbrwPO po = new RsdbgldbrwPO();
        BeanUtils.copyProperties(dto, po);

        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);//最近修改人
        if(StringUtils.isBlank(id)){//新增
            //获取督办编号
            VSSgOrgPO orgPO = this.vSSgOrgMapper.selectById(orgid);
            String areaid = orgPO.getAreaid();
            po.setBae001(orgid);
            po.setBae002(userid);

            po = this.insertDbrw(po,areaid,currentTime);

        }else{//修改
            this.dbrwMapper.updateById(po);
        }
        return po.getRsdbgldbrwlsh();
    }

    /**
     *新增督办任务
     * @param areaid
     * @param currentTime
     * @return
     */
    private synchronized RsdbgldbrwPO insertDbrw(RsdbgldbrwPO po,String areaid,String currentTime){
        String dbbh="";
        po.setDbbh(dbbh);
        this.dbrwMapper.insert(po);
        return po;
    }

    @Override
    public void updateDbrwPf(DbrwglDbrwCommDTO dto){
        if(StringUtils.isBlank(dto.getPfzt())){
            throw new AppException("派发状态不能为空");
        }
        //派发、取消派发
        RsdbgldbrwPO po = new RsdbgldbrwPO();
        po.setPfzt(dto.getPfzt());
        if("0".equals(dto.getPfzt())){//取消派发
            po.setPfr("");
            po.setPfsj("");
        }
        this.dbrwMapper.updateById(po);
    }

    @Override
    public void updateDbrwJs(DbrwglDbrwCommDTO dto){
        if(StringUtils.isBlank(dto.getRwzt())){
            throw new AppException("任务状态不能为空");
        }
        RsdbgldbrwPO po = new RsdbgldbrwPO();
        po.setRwzt(dto.getRwzt());
        if("0".equals(dto.getRwzt())){
            po.setJssj("");
            po.setJsr("");
        }
        this.dbrwMapper.updateById(po);
    }

    @Override
    public void deleteDbrwById(DbrwglDbrwCommDTO dto){
        RsdbgldbrwPO po = new RsdbgldbrwPO();
        po.setTsyxbz("0");

        this.dbrwMapper.updateById(po);
    }

    @Override
    public void saveDbrwpj(DbrwglDbrwUPjDTO dto){
        RsdbgldbrwPO po = new RsdbgldbrwPO();
        BeanUtils.copyProperties(dto, po);

        String currentTime = this.commonBLO.getCurrenttime();        
        po.setPjsj(currentTime);
        this.dbrwMapper.updateById(po);
    }
    @Override
    public void saveXmxx(DbrwglXmxxSaveDTO dto){
        RsdbgldbxmPO po = new RsdbgldbxmPO();

        if("update".equals(dto.getOper())){
            if(StringUtils.isBlank(dto.getRsdbgldbxmlsh())){
                throw new AppException("修改时，项目流水号不能为空");
            }
            po.setRsdbgldbxmlsh(dto.getRsdbgldbxmlsh());
            po.setXmmc(dto.getXmmc());
            this.dbxmMapper.updateById(po);
        }
    }

    public static void main(String[] args) {
        String str="010203";
        System.out.println(str.substring(str.length()-2,str.length()));
        System.out.println(str.substring(0,str.length()-2));
    }
    @Override
    public void delXmxx(DbrwglXmxxCommDTO dto){
        RsdbgldbxmPO po = new RsdbgldbxmPO();

        CurrentUser currentUser = getCurrentUser();
        po.setTsyxbz("0");

        RsdbgldbxmPO xmPO= this.dbxmMapper.selectById(dto.getIds());
        String xmbh = xmPO.getXmbh();

        QueryWrapper<RsdbgldbxmPO> queryWrapper = new QueryWrapper<RsdbgldbxmPO>();
        queryWrapper.eq("rs_dbgl_dbrw_lsh",xmPO.getRsdbgldbrwlsh())
                .eq("ts_yxbz", "1")
                .likeRight("xmbh",xmbh);
        this.dbxmMapper.update(po,queryWrapper);
        //判断父节点是否为叶子节点，设置isleaf
        if(xmbh.length() >= 4){
            String parentXmbh = xmbh.substring(0,xmbh.length() -2);
            QueryWrapper<RsdbgldbxmPO> queryWrapper2 = new QueryWrapper<RsdbgldbxmPO>();           
            int cnt = this.dbxmMapper.selectCount(queryWrapper2);
            if(cnt == 1){//父节点为叶子节点
                RsdbgldbxmPO rdbgldbxmPO = new RsdbgldbxmPO();
                rdbgldbxmPO.setIsleaf("1");
                QueryWrapper<RsdbgldbxmPO> queryWrapper3 = new QueryWrapper<RsdbgldbxmPO>();
                this.dbxmMapper.update(rdbgldbxmPO,queryWrapper3);
            }
        }


    }

    @Override
    public void saveXmxxQtbm(DbrwglXmxxCommDTO dto){
        RsdbgldbxmPO po = new RsdbgldbxmPO();


        po.setRsdbgldbxmlsh(dto.getIds());
        //先删除
        QueryWrapper<RsdbgldbxmzrbmPO> zrbmqueryWrapper = new QueryWrapper<RsdbgldbxmzrbmPO>();
        this.dbxmZrbmMapper.delete(zrbmqueryWrapper);

        if(StringUtils.isNotBlank(dto.getQtbm())){//设置牵头部门
            RsdbgldbxmzrbmPO zrbmPO = new RsdbgldbxmzrbmPO();
            zrbmPO.setRsdbgldbxmlsh(dto.getIds());
            this.dbxmZrbmMapper.insert(zrbmPO);
        }

        this.dbxmMapper.updateById(po);
    }


    @Override
    public void saveXmxxXbbm(DbrwglXmxxCommDTO dto){
        RsdbgldbxmPO po = new RsdbgldbxmPO();

        CurrentUser currentUser = getCurrentUser();
        po.setRsdbgldbxmlsh(dto.getIds());
        //先删除
        QueryWrapper<RsdbgldbxmzrbmPO> zrbmqueryWrapper = new QueryWrapper<RsdbgldbxmzrbmPO>();
        this.dbxmZrbmMapper.delete(zrbmqueryWrapper);        
        this.dbxmMapper.updateById(po);
    }

    @Override
    public RsdbgldbxmPO getXmxxById(DbrwglXmxxCommDTO dto){
        return this.dbxmMapper.selectById(dto.getIds());
    }

    @Override
    public RsdbgldbrwPO getDbrwxxById(DbrwglCommDTO dto){
        RsdbgldbrwPO po = this.dbrwMapper.selectById(dto.getIds());
        return po;
    }
    @Override
    public PageResult getXmxxList(DbrwglXmxxQueryDTO dto, Integer pagenum, Integer pagesize){
        Page<Map<String, Object>> page = new Page<>(dto.getPagenum(), dto.getPagesize());
        CurrentUser currentUser = getCurrentUser();
        String orgid = currentUser.getOrgid()==null?"":currentUser.getOrgid().trim();
        
        Page<Map<String, Object>> pageResult = (Page) dbrwMapper.getXmxxList(page,dto);

        //处理分管领导字段
        List<Map<String, Object>> list = pageResult.getRecords();
        if(list !=null && !list.isEmpty()){
            for(Map<String, Object> map : list){
                String fgld = this.dbrwglCommPLO.getFgldByXmId((String)map.get("rsdbgldbxmlsh"));
                map.put("fgld",fgld);
            }
        }

        PageResult result = PageUtil.PageResult(pageResult);
        return result;
    }

    @Override
    public String getXmxxQtbmByXmid(DbrwglXmxxCommDTO dto){
        QueryWrapper<RsdbgldbxmzrbmPO> zrbmqueryWrapper = new QueryWrapper<RsdbgldbxmzrbmPO>();
        RsdbgldbxmzrbmPO po = this.dbxmZrbmMapper.selectOne(zrbmqueryWrapper);
        if(po != null){
            return po.getOrgid();
        }
        return "";
    }

    @Override
    public String getXmxxXbbmByXmid(DbrwglXmxxCommDTO dto){
        QueryWrapper<RsdbgldbxmzrbmPO> zrbmqueryWrapper = new QueryWrapper<RsdbgldbxmzrbmPO>();
        List<RsdbgldbxmzrbmPO> polist = this.dbxmZrbmMapper.selectList(zrbmqueryWrapper);
        String xbmbs = "";
        if(polist != null && !polist.isEmpty()){
            for(RsdbgldbxmzrbmPO po : polist){
                xbmbs += po.getOrgid() + ",";
            }
        }
        if(StringUtils.isNotBlank(xbmbs)){
            xbmbs = xbmbs.substring(0,xbmbs.length() -1);
        }
        return xbmbs;

    }
}
