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.common.pojo.CommonResult;
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.pojo.domain.TxwGxzxRzsqjlbDO;
import com.css.txw.gxzx.pojo.vo.QyRzSqxxVO;
import com.css.txw.gxzx.pojo.vo.QyrzListReqVO;
import com.css.txw.mhzc.api.IMhzcApi;
import com.css.txw.gxzx.mapper.TxwGxzxRzsqjlbMapper;
import com.css.txw.gxzx.pojo.vo.QyRzSpReqVO;
import com.css.txw.gxzx.pojo.vo.QyRzSqReqVO;
import com.css.txw.gxzx.service.TxwGxzxRzsqjlbService;
import com.css.txw.mhzc.pojo.QyrzReqDTO;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author AirlH
* @description 针对表【txw_gxzx_rzsqjlb(入驻申请记录表)】的数据库操作Service实现
* @createDate 2025-09-12 15:55:04
*/
@Service
public class TxwGxzxRzsqjlbServiceImpl extends ServiceImpl<TxwGxzxRzsqjlbMapper, TxwGxzxRzsqjlbDO>
    implements TxwGxzxRzsqjlbService {

    @Resource
    private TxwGxzxRzsqjlbMapper rzsqjlbMapper;

    @Resource
    private IMhzcApi iMhzcApi;

    @Resource
    private XxzxApi xxzxApi;

    @Override
    public String qyrzsq(QyRzSqReqVO reqVO) {
        TxwGxzxRzsqjlbDO rzsqjlbDO = BeanUtils.toBean(reqVO,TxwGxzxRzsqjlbDO.class);
        rzsqjlbDO.setUuid(IdUtil.fastSimpleUUID());
        rzsqjlbDO.setLrrq(new Date());
        rzsqjlbDO.setLrruuid(SessionUtils.getYhUuid());
        rzsqjlbDO.setYwlxDm("01");
        rzsqjlbDO.setSqly(reqVO.getSqsm());
        
        rzsqjlbDO.setYhUuid(SessionUtils.getYhUuid()); 
        rzsqjlbDO.setDlzh(SessionUtils.getDlzh());
        rzsqjlbDO.setZsxm1(SessionUtils.getZsxm());
        this.save(rzsqjlbDO);
        return "ok";
    }

    @Transactional
    @Override
    public String qyrzsp(QyRzSpReqVO reqVO) {
        TxwGxzxRzsqjlbDO rzsqjlbDO = getById(reqVO.getUuid());
        LocalDateTime now = LocalDateTime.now();

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

        // 生成时间字符串
        String timeString = now.format(formatter);
        if ("3".equals(reqVO.getShjg1())){
            QyrzReqDTO reqDTO = new QyrzReqDTO();
            reqDTO.setYwlxDm("01");
            reqDTO.setNsrsbh(rzsqjlbDO.getNsrsbh());
            CommonResult<String> qyrz = iMhzcApi.qyrz(reqDTO);
            if (!qyrz.getData().equals("success")) throw new IllegalStateException("企业信息入驻标志修改错误");
            XxReqVO xxReqVO = new XxReqVO();
            Map<String,String> btMap = new HashMap<>();
            btMap.put("qymc",rzsqjlbDO.getQymc());
            Map<String,String> nrMap = new HashMap<>();
            nrMap.put("qymc",rzsqjlbDO.getQymc());
            nrMap.put("rq",timeString);
            xxReqVO.setMbuuid("gxdt_rzsp_sptg");
            xxReqVO.setXxbtMap(btMap);
            xxReqVO.setXxnrMap(nrMap);
            xxReqVO.setYhuuid(rzsqjlbDO.getLrruuid());
            xxzxApi.xtxxfb(xxReqVO);
        }else {
            XxReqVO xxReqVO = new XxReqVO();
            Map<String,String> btMap = new HashMap<>();
            btMap.put("qymc",rzsqjlbDO.getQymc());
            Map<String,String> nrMap = new HashMap<>();
            nrMap.put("qymc",rzsqjlbDO.getQymc());
            nrMap.put("rq",timeString);
            xxReqVO.setMbuuid("gxdt_rzsp_spsb");
            xxReqVO.setXxbtMap(btMap);
            xxReqVO.setXxnrMap(nrMap);
            xxReqVO.setYhuuid(rzsqjlbDO.getLrruuid());
            xxzxApi.xtxxfb(xxReqVO);
        }

        rzsqjlbDO.setShjg1(reqVO.getShjg1());
        rzsqjlbDO.setShyj1(reqVO.getShyj1());
        rzsqjlbDO.setShrsfid(SessionUtils.getYhUuid());
        rzsqjlbDO.setShsj(new Date());
        updateById(rzsqjlbDO);
        return "";
    }

    @Override
    public List<QyRzSqxxVO> qyrzList(QyrzListReqVO reqVO) {
        List<TxwGxzxRzsqjlbDO> txwGxzxRzsqjlbDOS = rzsqjlbMapper.qyrzList(reqVO);
        if (GyUtils.isNull(txwGxzxRzsqjlbDOS)) return null;
        return BeanUtils.toBean(txwGxzxRzsqjlbDOS,QyRzSqxxVO.class);
    }

    @Override
    public List<QyRzSqxxVO> currentQyrzList() {
        List<TxwGxzxRzsqjlbDO> txwGxzxRzsqjlbDOS = rzsqjlbMapper.currentQyrzList(SessionUtils.getYhUuid());
        if (GyUtils.isNull(txwGxzxRzsqjlbDOS)) return null;
        return BeanUtils.toBean(txwGxzxRzsqjlbDOS,QyRzSqxxVO.class);
    }
}




