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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.cache.utils.XtcsUtils;
import com.css.ggzc.framework.cache.vo.DmbDTO;
import com.css.ggzc.framework.common.exception.RRException;
import com.css.ggzc.framework.common.pojo.PageResult;
import com.css.ggzc.framework.common.util.gy.GyUtils;
import com.css.ggzc.framework.common.util.json.JsonUtils;
import com.css.ggzc.framework.session.SessionUtils;
import com.css.txw.gxzx.constants.SjztEnum;
import com.css.txw.gxzx.mapper.lsjy.GxzxLsjyZcxxMapper;
import com.css.txw.gxzx.pojo.domain.TxwGxzxShqkbDO;
import com.css.txw.gxzx.pojo.domain.lsjy.GxzxLsjyZcxx;
import com.css.txw.gxzx.pojo.lsjy.BatchDeleteZcxxReqVO;
import com.css.txw.gxzx.pojo.lsjy.GxzxLsjyZcxxDto;
import com.css.txw.gxzx.pojo.lsjy.ZcsxjReqVO;
import com.css.txw.gxzx.pojo.lsjy.ZcxxcxRequestVO;
import com.css.txw.gxzx.pojo.vo.GxspReqVO;
import com.css.txw.gxzx.pojo.vo.ShqkxxVO;
import com.css.txw.gxzx.service.TxwGxzxShqkbService;
import com.css.txw.gxzx.service.lsjy.GxzxLsjyZcxxService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class GxzxLsjyZcxxServiceImpl extends ServiceImpl<GxzxLsjyZcxxMapper, GxzxLsjyZcxx> implements GxzxLsjyZcxxService {

    @Resource
    private TxwGxzxShqkbService txwGxzxShqkbService;

    @Resource
    private TxwGxzxShqkbService shqkbService;

    @Override
    public List<DmbDTO> queryZclxList() {
        String tableName = "LSJY_ZCLX";
        List<DmbDTO> dtoList = CacheUtils.getDmbData(tableName);
        if (GyUtils.isNull(dtoList)) {
            dtoList = new ArrayList<>();
        }
        return dtoList;
    }

    @Override
    public PageResult<GxzxLsjyZcxxDto> queryZcxxList(ZcxxcxRequestVO requestVO) {
        List<GxzxLsjyZcxxDto> list = new ArrayList<>();
        PageResult<GxzxLsjyZcxxDto> pageResult = new PageResult<>();
        pageResult.setTotal(0L);
        Integer pageNum = requestVO.getPageNum();
        Integer pageSize = requestVO.getPageSize();
        String zclxdm = requestVO.getZclxdm();
        BigDecimal zddj = requestVO.getZddj(); // 最低价
        BigDecimal zgdj = requestVO.getZgdj(); // 最高价
        String nsrsbh = requestVO.getNsrsbh();
        String nsrmc = requestVO.getNsrmc();
        String yxqz = requestVO.getYxqz();
        String module = requestVO.getModule();
        String sxjzt = requestVO.getSxjzt();
        String qyuuid = SessionUtils.getQyuuid();
        Page<GxzxLsjyZcxx> page = new Page<>(pageNum, pageSize);
        QueryWrapper<GxzxLsjyZcxx> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                // 绿色交易或者后台管理页面查询时只查询审批通过的数据
              //  .eq((GyUtils.isNull(module) || "wh".equals(module)), GxzxLsjyZcxx::getZt, "3")
                // 审批页面只查询待审批数据
                .eq((GyUtils.isNotNull(module) && "sp".equals(module)), GxzxLsjyZcxx::getZt, "2")
                // 绿色交易页面只查询上架数据
                .eq(GyUtils.isNull(module), GxzxLsjyZcxx::getSjzt, SjztEnum.SJ.getCode())
               // 门户中心 查询自己填加的数据
                .eq(GyUtils.isEquals(module, "yhwh"), GxzxLsjyZcxx::getLrruuid, qyuuid)
                .eq(GyUtils.isNotNull(zclxdm),GxzxLsjyZcxx::getZclxdm, zclxdm)
                .eq(GyUtils.isNotNull(nsrsbh), GxzxLsjyZcxx::getQyuuid, nsrsbh)
                .like(GyUtils.isNotNull(nsrmc), GxzxLsjyZcxx::getQymc, nsrmc)
                .ge(GyUtils.isNotNull(zddj),GxzxLsjyZcxx::getDj3, zddj)
                .le(GyUtils.isNotNull(zgdj), GxzxLsjyZcxx::getDj3, zgdj)
                .le(GyUtils.isNotNull(yxqz), GxzxLsjyZcxx::getYxqz, yxqz)
                .eq(GyUtils.isNotNull(sxjzt), GxzxLsjyZcxx::getSjzt, sxjzt)
                .eq(GyUtils.isNotNull(requestVO.getZt()), GxzxLsjyZcxx::getZt, requestVO.getZt())
                .orderByDesc(GxzxLsjyZcxx::getXgrq);
        Page<GxzxLsjyZcxx> zcxxPage = this.page(page, queryWrapper);
        if (GyUtils.isNotNull(zcxxPage) && GyUtils.isNotNull(zcxxPage.getRecords())) {
            List<GxzxLsjyZcxx> records = zcxxPage.getRecords();
            list = JsonUtils.toList(JsonUtils.toJson(records), GxzxLsjyZcxxDto.class);
            String tableName = "LSJY_ZCLX";
            List<DmbDTO> dtoList = CacheUtils.getDmbData(tableName);
            Map<String, String> zclxMap = dtoList.stream().collect(Collectors.toMap(DmbDTO::getCode, DmbDTO::getCaption));
            for (GxzxLsjyZcxxDto dto : list) {
                String zclxdm1 = dto.getZclxdm();
                String zclxmc = zclxMap.get(zclxdm1);
                dto.setZclxmc(zclxmc);
            }
        }
        pageResult.setTotal(zcxxPage.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Override
    public GxzxLsjyZcxxDto queryZcxxxq(String zcid) {
        GxzxLsjyZcxx zcxx = this.getById(zcid);
        GxzxLsjyZcxxDto zcxxDto = JsonUtils.toBean(JsonUtils.toJson(zcxx), GxzxLsjyZcxxDto.class);
        String tableName = "LSJY_ZCLX";
        String zclxmc = CacheUtils.dm2mc(tableName, zcxxDto.getZclxdm());
        zcxxDto.setZclxmc(zclxmc);
        return zcxxDto;
    }

    @Override
    public String saveOrUpdate(GxzxLsjyZcxxDto dto) {
        String qyuuid = SessionUtils.getQyuuid();
        String qymc = SessionUtils.get("qymc").toString();
        String shxydm = SessionUtils.getShxydm();
        String zcid = dto.getZcid();
        String module = dto.getModule();// 管理员模式可直接修改产品信息，去掉只能修改本企业产品信息限制
        if (GyUtils.isNotNull(zcid)) {
            QueryWrapper<GxzxLsjyZcxx> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda()
                    .eq(GxzxLsjyZcxx::getZcid, zcid)
                    .eq(!"manager".equals(module), GxzxLsjyZcxx::getQyuuid, qyuuid)
                    .last("limit 1");
            GxzxLsjyZcxx one = this.getOne(queryWrapper);
            if (GyUtils.isNull(one)) {
                throw new RRException("未查询到符合修改条件的资产数据");
            }
            dto.setQymc(qymc);
            dto.setXgrq(new Date());
            dto.setXgruuid(qyuuid);
            dto.setDj3(NumberUtil.toBigDecimal(dto.getDjStr()));
        } else {
            zcid = IdUtil.fastSimpleUUID();
            dto.setZcid(zcid);
            dto.setQymc(qymc);
            dto.setQyuuid(shxydm);
            dto.setZcsysl(dto.getZcsl());
            dto.setLrrq(new Date());
            dto.setXgrq(dto.getLrrq());
            dto.setLrruuid(qyuuid);
            dto.setDj3(NumberUtil.toBigDecimal(dto.getDjStr()));
        }
        GxzxLsjyZcxx zcxx = BeanUtil.toBean(dto, GxzxLsjyZcxx.class);
        zcxx.setZt("2");
        zcxx.setSjzt(SjztEnum.WSJ.getCode());
        zcxx.setFbrq(null);
        String sfsh = XtcsUtils.getXtcs("LSJR_SFSH", "Y");
        if ("N".equals(sfsh)) {
            zcxx.setZt("3");
            zcxx.setSjzt(SjztEnum.SJ.getCode());
            zcxx.setFbrq(new Date());
        } else {
            TxwGxzxShqkbDO shqkbDO = new TxwGxzxShqkbDO();
            shqkbDO.setUuid(IdUtil.fastSimpleUUID());
            shqkbDO.setYwlxDm("zc");
            shqkbDO.setGlid(zcxx.getZcid());
            shqkbDO.setLrrq(new Date());
            shqkbDO.setLrrsfid(qyuuid);
            txwGxzxShqkbService.save(shqkbDO);
        }
        this.saveOrUpdate(zcxx);
        return zcxx.getZcid();
    }

    @Override
    public boolean batchDelete(BatchDeleteZcxxReqVO reqVO) {
        String zcuuids = reqVO.getZcuuids();
        boolean b = this.removeBatchByIds(Arrays.asList(zcuuids.split(",")));
        return b;
    }

    @Override
    public PageResult<ShqkxxVO> queryDspZcxxList(ZcxxcxRequestVO requestVO) {
        List<ShqkxxVO> list = new ArrayList<>();
        PageResult<ShqkxxVO> pageResult = new PageResult<>();
        pageResult.setTotal(0L);
        Integer pageNum = requestVO.getPageNum();
        Integer pageSize = requestVO.getPageSize();
        Page<TxwGxzxShqkbDO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<TxwGxzxShqkbDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TxwGxzxShqkbDO::getYwlxDm, "zc")
                .orderByDesc(TxwGxzxShqkbDO::getLrrq)
                .orderByAsc(TxwGxzxShqkbDO::getShjg1);
        Page<TxwGxzxShqkbDO> cpxxPage = txwGxzxShqkbService.page(page, queryWrapper);
        if (GyUtils.isNotNull(cpxxPage) && GyUtils.isNotNull(cpxxPage.getRecords())) {
            List<TxwGxzxShqkbDO> records = cpxxPage.getRecords();
            list = JsonUtils.toList(JsonUtils.toJson(records), ShqkxxVO.class);
        }
        pageResult.setTotal(cpxxPage.getTotal());
        pageResult.setList(list);
        return pageResult;
    }

    @Override
    public String zcSp(GxspReqVO reqVO) {
        TxwGxzxShqkbDO shqkbDO = txwGxzxShqkbService.getById(reqVO.getUuid());
        if (GyUtils.isNull(shqkbDO)) {
            return "未查询到待审核数据";
        }
        shqkbDO.setShrsfid(SessionUtils.getYhUuid());
        shqkbDO.setShjg1(reqVO.getShjg1());
        shqkbDO.setShyj1(reqVO.getShyj1());
        shqkbDO.setShsj(new Date());
        txwGxzxShqkbService.updateById(shqkbDO);
        String glid = shqkbDO.getGlid();
        GxzxLsjyZcxx zcxx = new GxzxLsjyZcxx();
        zcxx.setZcid(glid);
        zcxx.setZt(reqVO.getShjg1());
        if ("3".equals(reqVO.getShjg1())) {
            zcxx.setSjzt(SjztEnum.SJ.getCode());
            zcxx.setFbrq(new Date());
        }
        this.saveOrUpdate(zcxx);
        return "success";
    }

    @Override
    public String zcsxj(ZcsxjReqVO reqVO) {
        String zcids = reqVO.getZcids();
        List<String> zcidList = Arrays.asList(zcids.split(","));
        UpdateWrapper<GxzxLsjyZcxx> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .set(GxzxLsjyZcxx::getSjzt, reqVO.getSjzt())
                .set(GxzxLsjyZcxx::getXgrq, new Date())
                .set(GxzxLsjyZcxx::getXgruuid, SessionUtils.getYhUuid())
                .in(GxzxLsjyZcxx::getZcid, zcidList);
        this.update(updateWrapper);
        return "success";
    }
}