package com.css.txw.gxzx.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.css.ggzc.framework.cache.utils.CacheUtils;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.common.util.object.BeanUtils;
import com.css.ggzc.framework.session.SessionUtils;
import com.css.txw.gxzx.mapper.*;
import com.css.txw.gxzx.pojo.domain.*;
import com.css.txw.gxzx.pojo.vo.*;
import com.css.txw.gxzx.service.TxwGxzxShqkbService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author AirlH
* @description 针对表【txw_gxzx_shqkb(审核情况表)】的数据库操作Service实现
* @createDate 2025-09-12 15:19:45
*/
@Service
public class TxwGxzxShqkbServiceImpl extends ServiceImpl<TxwGxzxShqkbMapper, TxwGxzxShqkbDO>
    implements TxwGxzxShqkbService {

    @Resource
    private TxwGxzxShqkbMapper shqkbMapper;

    @Resource
    private TxwGxzxGxxxbMapper gxxxbMapper;

    @Resource
    private TxwGxzxGxbqbMapper gxbqbMapper;

    @Resource
    private TxwGxzxGxscbMapper gxscbMapper;

    @Override
    public List<ShqkxxVO> gxfbList() {
        List<TxwGxzxShqkbDO> txwGxzxShqkbDOS = shqkbMapper.gxfbList();
        if (GyUtils.isNull(txwGxzxShqkbDOS)) return null;
        return BeanUtils.toBean(txwGxzxShqkbDOS, ShqkxxVO.class);
    }

    @Override
    public String gxfbSp(GxspReqVO reqVO) {
        TxwGxzxShqkbDO shqkbDO = getById(reqVO.getUuid());
        shqkbDO.setShrsfid(SessionUtils.getYhUuid());
        shqkbDO.setShjg1(reqVO.getShjg1());
        shqkbDO.setShyj1(reqVO.getShyj1());
        shqkbDO.setShsj(new Date());
        updateById(shqkbDO);
        return "success";
    }

    @Override
    public Page<GxxxVO> gxxxList(GxxxReqVO reqVO) {
        List<TxwGxzxGxscbDO> byYhuuid = gxscbMapper.getByYhuuid(SessionUtils.getYhUuid());
        List<String> scgxuuid = GyUtils.isNull(byYhuuid)?new ArrayList<>():byYhuuid.stream().map(TxwGxzxGxscbDO::getGxUuid).collect(Collectors.toList());

        Set<String> gxuuids = new LinkedHashSet<>();
        if (!GyUtils.isNull(reqVO.getFwlxjh())){
            List<TxwGxzxGxbqbDO> list = gxbqbMapper.getGxuuids(reqVO.getQyuuids(),reqVO.getFwlxjh());
            if (GyUtils.isNull(list)) return null;
            gxuuids = list.stream().map(TxwGxzxGxbqbDO::getGxUuid).collect(Collectors.toSet());
            if (GyUtils.isNull(gxuuids)||gxuuids.size()<1) return null;
            if (!GyUtils.isNull(reqVO.getScbz())&&"Y".equals(reqVO.getScbz())){
                gxuuids.retainAll(scgxuuid);
                if (GyUtils.isNull(gxuuids)) return null;
            }
        }else {
            if (!GyUtils.isNull(reqVO.getScbz())&&"Y".equals(reqVO.getScbz())){
                gxuuids.addAll(scgxuuid);
                if (GyUtils.isNull(gxuuids)) return null;
            }
        }

        Page<TxwGxzxGxxxbDO> txwGxzxGxxxbDOS = gxxxbMapper.gxxxList(reqVO, gxuuids);
        if (GyUtils.isNull(txwGxzxGxxxbDOS)) return null;
        List<TxwGxzxGxxxbDO> records = txwGxzxGxxxbDOS.getRecords();
        if (GyUtils.isNull(records)) return null;
        List<GxxxVO> gxxxVOS = BeanUtils.toBean(records, GxxxVO.class);
        List<GxxxVO> collect = gxxxVOS.stream().map(m -> {
            m.setScbz("N");
            if (scgxuuid.contains(m.getGxUuid())) m.setScbz("Y");
            if (!GyUtils.isNull(m.getBqjh())) {
                String bqjh = m.getBqjh();
                String[] split = bqjh.split(",");
                StringBuilder bqjhmc = new StringBuilder();
                for (String s : split) {
                    String bq = CacheUtils.dm2mc("bq", s);
                    bqjhmc.append(bq).append(",");
                }
                bqjhmc.deleteCharAt(bqjhmc.length() - 1);
                m.setBqjh(bqjhmc.toString());
            }
            if (!GyUtils.isNull(m.getFwfw())) {
                m.setFwfw(CacheUtils.dm2mc("xzqh", m.getFwfw()));
            }
            if (!GyUtils.isNull(m.getFwlxjh())){
                String fwlx = m.getFwlxjh();
                String[] split = fwlx.split(",");
                StringBuilder fwlxjhmc = new StringBuilder();
                for (String s : split) {
                    String fwlxmc = m.getYwlxDm().equals("01")?CacheUtils.dm2mc("fwlx", s):CacheUtils.dm2mc("xqlx", s);
                    fwlxjhmc.append(fwlxmc).append(",");
                }
                fwlxjhmc.deleteCharAt(fwlxjhmc.length()-1);
                m.setFwlxjh(fwlxjhmc.toString());
            }
            if (!GyUtils.isNull(m.getGjdwDm())) {
                m.setGjdwDm(CacheUtils.dm2mc("dw", m.getGjdwDm()));
            }
//            if (!GyUtils.isNull(m.getSshy())){
//                m.setSshy(CacheUtils.dm2mc("sshy",m.getSshy()));
//            }
            if (!GyUtils.isNull(m.getYsfwDm1())) {
                m.setYsfwDm1(CacheUtils.dm2mc("ysfw", m.getYsfwDm1()));
            }
            if (!GyUtils.isNull(m.getXqjjcdDm())) {
                m.setXqjjcdDm(CacheUtils.dm2mc("jjcd", m.getXqjjcdDm()));
            }
            return m;
        }).collect(Collectors.toList());
        Page<GxxxVO> page = new Page<>();
        page.setRecords(collect);
        page.setSize(txwGxzxGxxxbDOS.getSize());
        page.setPages(txwGxzxGxxxbDOS.getPages());
        page.setTotal(txwGxzxGxxxbDOS.getTotal());
        page.setCurrent(txwGxzxGxxxbDOS.getCurrent());
        return page;
    }

    @Override
    public List<String> getQyuuidsByBq(GxxxReqVO reqVO) {
        List<TxwGxzxGxbqbDO> list = gxbqbMapper.getQyuuids(reqVO.getFwlxjh());
        if (GyUtils.isNull(list)) return null;
        return list.stream().map(TxwGxzxGxbqbDO::getQyuuid).collect(Collectors.toList());
    }

    @Override
    public Page<GxxxVO> gxxxListByYh(GxxxReqVO reqVO) {
        Page<TxwGxzxGxxxbDO> txwGxzxGxxxbDOS = gxxxbMapper.gxxxListByYh(reqVO,SessionUtils.getYhUuid());
        if (GyUtils.isNull(txwGxzxGxxxbDOS)) return null;
        List<TxwGxzxGxxxbDO> records = txwGxzxGxxxbDOS.getRecords();
        if (GyUtils.isNull(records)) return null;
        List<GxxxVO> gxxxVOS = BeanUtils.toBean(records, GxxxVO.class);
        List<GxxxVO> collect = gxxxVOS.stream().map(m -> {
            if (!GyUtils.isNull(m.getBqjh())) {
                String bqjh = m.getBqjh();
                String[] split = bqjh.split(",");
                StringBuilder bqjhmc = new StringBuilder();
                for (String s : split) {
                    String bq = CacheUtils.dm2mc("bq", s);
                    bqjhmc.append(bq).append(",");
                }
                bqjhmc.deleteCharAt(bqjhmc.length() - 1);
                m.setBqjh(bqjhmc.toString());
            }
            if (!GyUtils.isNull(m.getFwfw())) {
                m.setFwfw(CacheUtils.dm2mc("xzqh", m.getFwfw()));
            }
            if (!GyUtils.isNull(m.getFwlxjh())){
                String fwlx = m.getFwlxjh();
                String[] split = fwlx.split(",");
                StringBuilder fwlxjhmc = new StringBuilder();
                for (String s : split) {
                    String fwlxmc = m.getYwlxDm().equals("01")?CacheUtils.dm2mc("fwlx", s):CacheUtils.dm2mc("xqlx", s);
                    fwlxjhmc.append(fwlxmc).append(",");
                }
                fwlxjhmc.deleteCharAt(fwlxjhmc.length()-1);
                m.setFwlxjh(fwlxjhmc.toString());
            }
            if (!GyUtils.isNull(m.getGjdwDm())) {
                m.setGjdwDm(CacheUtils.dm2mc("dw", m.getGjdwDm()));
            }
            if (!GyUtils.isNull(m.getSshy())) {
                m.setSshy(CacheUtils.dm2mc("sshy", m.getSshy()));
            }
            if (!GyUtils.isNull(m.getYsfwDm1())) {
                m.setYsfwDm1(CacheUtils.dm2mc("ysfw", m.getYsfwDm1()));
            }
            if (!GyUtils.isNull(m.getXqjjcdDm())) {
                m.setXqjjcdDm(CacheUtils.dm2mc("jjcd", m.getXqjjcdDm()));
            }
            if (!GyUtils.isNull(m.getZt())) {
                m.setZt(CacheUtils.dm2mc("shzt", m.getZt()));
            }
            return m;
        }).collect(Collectors.toList());

        Page<GxxxVO> page = new Page<>();
        page.setRecords(collect);
        page.setSize(txwGxzxGxxxbDOS.getSize());
        page.setPages(txwGxzxGxxxbDOS.getPages());
        page.setTotal(txwGxzxGxxxbDOS.getTotal());
        page.setCurrent(txwGxzxGxxxbDOS.getCurrent());
        return page;
    }

    @Override
    public Page<GxxxVO> getGxSqList(GetGxSqListReqVO reqVO) {
        Page<TxwGxzxGxxxbDO> gxSqList = gxxxbMapper.getGxSqList(reqVO);
        if (GyUtils.isNull(gxSqList)) return null;
        List<TxwGxzxGxxxbDO> records = gxSqList.getRecords();
        List<GxxxVO> gxxxVOS = BeanUtils.toBean(records, GxxxVO.class);
        List<GxxxVO> collect = gxxxVOS.stream().map(m -> {
            if (!GyUtils.isNull(m.getBqjh())) {
                String bqjh = m.getBqjh();
                String[] split = bqjh.split(",");
                StringBuilder bqjhmc = new StringBuilder();
                for (String s : split) {
                    String bq = CacheUtils.dm2mc("bq", s);
                    bqjhmc.append(bq).append(",");
                }
                bqjhmc.deleteCharAt(bqjhmc.length() - 1);
                m.setBqjh(bqjhmc.toString());
            }
            if (!GyUtils.isNull(m.getFwfw())) {
                m.setFwfw(CacheUtils.dm2mc("xzqh", m.getFwfw()));
            }
            if (!GyUtils.isNull(m.getFwlxjh())){
                String fwlx = m.getFwlxjh();
                String[] split = fwlx.split(",");
                StringBuilder fwlxjhmc = new StringBuilder();
                for (String s : split) {
                    String fwlxmc = m.getYwlxDm().equals("01")?CacheUtils.dm2mc("fwlx", s):CacheUtils.dm2mc("xqlx", s);
                    fwlxjhmc.append(fwlxmc).append(",");
                }
                fwlxjhmc.deleteCharAt(fwlxjhmc.length()-1);
                m.setFwlxjh(fwlxjhmc.toString());
            }
            if (!GyUtils.isNull(m.getGjdwDm())) {
                m.setGjdwDm(CacheUtils.dm2mc("dw", m.getGjdwDm()));
            }
            if (!GyUtils.isNull(m.getSshy())) {
                m.setSshy(CacheUtils.dm2mc("sshy", m.getSshy()));
            }
            if (!GyUtils.isNull(m.getYsfwDm1())) {
                m.setYsfwDm1(CacheUtils.dm2mc("ysfw", m.getYsfwDm1()));
            }
            if (!GyUtils.isNull(m.getXqjjcdDm())) {
                m.setXqjjcdDm(CacheUtils.dm2mc("jjcd", m.getXqjjcdDm()));
            }
            if (!GyUtils.isNull(m.getZt())) {
                m.setZt(CacheUtils.dm2mc("shzt", m.getZt()));
            }
            return m;
        }).collect(Collectors.toList());
        Page<GxxxVO> page = new Page<>();
        page.setRecords(collect);
        page.setSize(gxSqList.getSize());
        page.setPages(gxSqList.getPages());
        page.setTotal(gxSqList.getTotal());
        page.setCurrent(gxSqList.getCurrent());
        return page;
//        Page<TxwGxzxShqkbDO> gxSqList = shqkbMapper.getGxSqList(reqVO);
//        if (GyUtils.isNull(gxSqList)) return null;
//        List<TxwGxzxShqkbDO> records = gxSqList.getRecords();
//        List<GxxxVO> collect = BeanUtils.toBean(records, GxxxVO.class);
//        Page<GxxxVO> page = new Page<>();
//        page.setRecords(collect);
//        page.setSize(gxSqList.getSize());
//        page.setPages(gxSqList.getPages());
//        page.setTotal(gxSqList.getTotal());
//        page.setCurrent(gxSqList.getCurrent());
//        return page;
    }
}