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

import cn.hutool.core.util.IdUtil;
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.ggzc.framework.xxzx.api.XxzxApi;
import com.css.ggzc.framework.xxzx.pojo.XxReqVO;
import com.css.txw.gxzx.mapper.TxwGxzxShqkbMapper;
import com.css.txw.gxzx.pojo.domain.TxwGxzxGxbqbDO;
import com.css.txw.gxzx.pojo.domain.TxwGxzxGxxxbDO;
import com.css.txw.gxzx.mapper.TxwGxzxGxxxbMapper;
import com.css.txw.gxzx.pojo.domain.TxwGxzxShqkbDO;
import com.css.txw.gxzx.pojo.vo.GxfbReqVO;
import com.css.txw.gxzx.pojo.vo.GxsjReqVO;
import com.css.txw.gxzx.pojo.vo.GxspReqVO;
import com.css.txw.gxzx.pojo.vo.GxxxVO;
import com.css.txw.gxzx.service.TxwGxzxGxbqbService;
import com.css.txw.gxzx.service.TxwGxzxGxxxbService;
import com.css.txw.gxzx.service.TxwGxzxShqkbService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
* @author AirlH
* @description 针对表【txw_gxzx_gxxxb(供需信息表)】的数据库操作Service实现
* @createDate 2025-09-13 11:26:24
*/
@Service
public class TxwGxzxGxxxbServiceImpl extends ServiceImpl<TxwGxzxGxxxbMapper, TxwGxzxGxxxbDO>
    implements TxwGxzxGxxxbService {

    @Resource
    private TxwGxzxShqkbService shqkbService;

    @Resource
    private TxwGxzxShqkbMapper shqkbMapper;

    @Resource
    private TxwGxzxGxbqbService gxbqbService;

    @Resource
    private XxzxApi xxzxApi;

    @Transactional
    @Override
    public String gxfb(GxfbReqVO reqVO) {
        TxwGxzxGxxxbDO gxxxbDO = BeanUtils.toBean(reqVO,TxwGxzxGxxxbDO.class);
        gxxxbDO.setGxUuid(IdUtil.fastSimpleUUID());
        gxxxbDO.setQyuuid(SessionUtils.getQyuuid());
        gxxxbDO.setQymc((String) SessionUtils.get("qymc"));
        gxxxbDO.setNsrsbh(SessionUtils.getNsrsbh());
        gxxxbDO.setSjzt("N");
        gxxxbDO.setZt("2");
        gxxxbDO.setLrrq(new Date());
        gxxxbDO.setLrruuid(SessionUtils.getYhUuid());
        save(gxxxbDO);
        TxwGxzxShqkbDO shqkbDO = new TxwGxzxShqkbDO();
        shqkbDO.setGlid(gxxxbDO.getGxUuid());
        shqkbDO.setYwlxDm(reqVO.getYwlxDm());
        shqkbDO.setUuid(IdUtil.fastSimpleUUID());
        shqkbDO.setLrrq(new Date());
        shqkbDO.setLrrsfid(SessionUtils.getYhUuid());
        shqkbService.save(shqkbDO);
        String fwlxjh = reqVO.getFwlxjh();
        saveQybq(gxxxbDO.getGxUuid(),SessionUtils.getQyuuid(), Arrays.asList(fwlxjh.split(",")));
        String zdspbz = CacheUtils.dm2mc("cs_ggzc_xtcs", "qyrzzdsp");
        if (!GyUtils.isNull(zdspbz)&&"Y".equals(zdspbz)){
            GxspReqVO req = new GxspReqVO();
            req.setGxuuid(gxxxbDO.getGxUuid());
            req.setYwlxDm(gxxxbDO.getYwlxDm());
            req.setZt("3");
            gxsp(req);
        }
        return "success";
    }

    @Override
    public String gxsj(GxsjReqVO reqVO) {
        TxwGxzxGxxxbDO gxxxbDO = BeanUtils.toBean(reqVO, TxwGxzxGxxxbDO.class);
        updateById(gxxxbDO);
        return "success";
    }

    private void saveQybq(String gxuuid,String qyuuid,List<String> bqxx){
        List<TxwGxzxGxbqbDO> list = new ArrayList<>();
        bqxx.forEach(v->{
            TxwGxzxGxbqbDO gxbqbDO = new TxwGxzxGxbqbDO();
            gxbqbDO.setGxUuid(gxuuid);
            gxbqbDO.setUuid(IdUtil.fastSimpleUUID());
            gxbqbDO.setYxbz("N");
            gxbqbDO.setBquuid(v);
            gxbqbDO.setLrrq(new Date());
            gxbqbDO.setQyuuid(qyuuid);
            list.add(gxbqbDO);
        });
        gxbqbService.saveBatch(list);
    }

    @Transactional
    @Override
    public void gxsp(GxspReqVO reqVO){
        TxwGxzxShqkbDO shqkbDO = shqkbMapper.getByGlidAndYwlx(reqVO.getGxuuid());
        TxwGxzxGxxxbDO byId = this.getById(reqVO.getGxuuid());
        TxwGxzxGxxxbDO gxxxbDO = new TxwGxzxGxxxbDO();
        gxxxbDO.setGxUuid(reqVO.getGxuuid());
        LocalDateTime now = LocalDateTime.now();

        // 定义格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 生成时间字符串
        String timeString = now.format(formatter);
        if ("3".equals(reqVO.getShjg1())){
            gxxxbDO.setSjzt("Y");
            XxReqVO xxReqVO = new XxReqVO();
            Map<String,String> btMap = new HashMap<>();
            btMap.put("gxbt",byId.getBt1());
            Map<String,String> nrMap = new HashMap<>();
            nrMap.put("gxbt",byId.getBt1());
            nrMap.put("rq",timeString);
            xxReqVO.setMbuuid("gxdt_gxsp_sptg");
            xxReqVO.setXxbtMap(btMap);
            xxReqVO.setXxnrMap(nrMap);
            xxReqVO.setYhuuid(shqkbDO.getLrrsfid());
            xxzxApi.xtxxfb(xxReqVO);
        }else {
            XxReqVO xxReqVO = new XxReqVO();
            Map<String,String> btMap = new HashMap<>();
            btMap.put("gxbt",byId.getBt1());
            Map<String,String> nrMap = new HashMap<>();
            nrMap.put("gxbt",byId.getBt1());
            nrMap.put("rq",timeString);
            xxReqVO.setMbuuid("gxdt_gxsp_spsb");
            xxReqVO.setXxbtMap(btMap);
            xxReqVO.setXxnrMap(nrMap);
            xxReqVO.setYhuuid(shqkbDO.getLrrsfid());
            xxzxApi.xtxxfb(xxReqVO);
        }
        gxxxbDO.setZt(reqVO.getShjg1());
        updateById(gxxxbDO);


        shqkbDO.setShrsfid(SessionUtils.getYhUuid());
        shqkbDO.setShjg1(reqVO.getShjg1());
        shqkbDO.setShyj1(reqVO.getShyj1());
        shqkbDO.setShsj(new Date());
        shqkbService.updateById(shqkbDO);

        gxbqbService.updYxbz(reqVO.getGxuuid());


    }

    @Override
    public GxxxVO getGxxx(String gxuuid) {
        TxwGxzxGxxxbDO gxxxbDO = this.getById(gxuuid);
        if (GyUtils.isNull(gxxxbDO)) return null;
        GxxxVO m = BeanUtils.toBean(gxxxbDO, GxxxVO.class);
        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;
    }

}




