package com.mosty.yjzl.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mosty.base.model.dto.base.SysDeptDTO;
import com.mosty.base.model.dto.yjzl.TbFzycDto;
import com.mosty.base.model.entity.yjzl.TbFzyc;
import com.mosty.base.model.entity.yjzl.TbFzycPz;
import com.mosty.base.model.vo.base.DeptInfoVo;
import com.mosty.base.model.vo.yjzl.ForecastInterfaceVO;
import com.mosty.base.utils.Constant;
import com.mosty.base.utils.DateUtils;
import com.mosty.base.utils.UUIDGenerator;
import com.mosty.common.core.business.service.SysRoleService;
import com.mosty.common.core.util.http.HttpUtils;
import com.mosty.common.redis.service.RedisService;
import com.mosty.common.token.DeptInfo;
import com.mosty.common.token.UserInfo;
import com.mosty.common.token.UserInfoManager;
import com.mosty.common.util.PermissionsUtil;
import com.mosty.yjzl.mapper.TbFzycMapper;
import com.mosty.yjzl.remote.TbBaseAdaptRemoteService;
import com.mosty.yjzl.service.TbFzycPzService;
import com.mosty.yjzl.service.TbFzycService;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@AllArgsConstructor
public class TbFzycServiceImpl extends ServiceImpl<TbFzycMapper, TbFzyc>
        implements TbFzycService {

    private final TbFzycPzService tbFzycPzService;
    private final SysRoleService sysRoleService;
    private final TbBaseAdaptRemoteService tbBaseAdaptRemoteService;
    private final RedisService redisService;

    @Override
    public int insert(TbFzyc fzyc) {
        fzyc.setId(UUIDGenerator.getUUID());
        fzyc.setSfxl("0");
        return this.baseMapper.insert(fzyc);
    }

    @Override
    public int fzUpdateById(TbFzyc fzyc) {
        return this.baseMapper.updateById(fzyc);
    }

    @Override
    public TbFzyc queryById(String id) {

        return this.baseMapper.selectById(id);
    }

    @Override
    public int deleteById(String id) {
        return this.baseMapper.deleteById(id);
    }

    @Override
    public List<TbFzyc> queryList(TbFzycDto tbFzycDto) {
        tbFzycDto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(tbFzycDto.getSsbmdm(), null));
        if (StringUtils.isBlank(tbFzycDto.getKssj())) {
            tbFzycDto.setKssj(DateUtils.getSystemDateString());
        }
//        if (CollectionUtils.isEmpty(tbFzycDto.getBcList())) {
//            tbFzycDto.setBc(redisService.getCacheObject(Constant.FZYC_BC));
//        }
        return this.baseMapper.selectListBm(tbFzycDto);
    }


    @Override
    public List<TbFzyc> queryListApp(TbFzycDto tbFzycDto) {
        tbFzycDto.setSsbmdm(this.tbBaseAdaptRemoteService.getSsbm(tbFzycDto.getSsbmdm(), null));
        if (StringUtils.isBlank(tbFzycDto.getKssj())) {
            tbFzycDto.setKssj(DateUtils.getSystemDateString());
        }
        //app只查看当前
        if (StringUtils.isEmpty(tbFzycDto.getBc())) {
            tbFzycDto.setBc(redisService.getCacheObject(Constant.FZYC_BC));
        }
        return this.baseMapper.selectListBm(tbFzycDto);
    }


    @Override
    public IPage<TbFzyc> queryListPage(TbFzycDto tbFzycDto) {
        QueryWrapper<TbFzyc> tb = new QueryWrapper<>();
        if (StringUtils.isNotBlank(tbFzycDto.getSsbmdm())) {
            String ssbm = this.tbBaseAdaptRemoteService.getSsbm(tbFzycDto.getSsbmdm(), null);
            tb.lambda().likeRight(StringUtils.isNotBlank(ssbm), TbFzyc::getSsbmdm, ssbm);
        } else if (StringUtils.isNotBlank(tbFzycDto.getSsbmid())) {
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(tbFzycDto.getSsbmid());
            String ssbm = this.tbBaseAdaptRemoteService.getSsbm(dept.getDeptcode(), null);
            tb.lambda().likeRight(StringUtils.isNotBlank(ssbm), TbFzyc::getSsbmdm, ssbm);
        } else {
            UserInfo user = UserInfoManager.getUser();
            DeptInfoVo dept = this.tbBaseAdaptRemoteService.getOrgByDeptId(user.getDeptCode());
            String ssbm = this.tbBaseAdaptRemoteService.getSsbm(dept.getDeptcode(), null);
            tb.lambda().likeRight(StringUtils.isNotBlank(ssbm), TbFzyc::getSsbmdm, ssbm);
        }
        tb.lambda().eq(StringUtils.isNotBlank(tbFzycDto.getSfxl()), TbFzyc::getSfxl, tbFzycDto.getSfxl())
                .eq(StringUtils.isNotBlank(tbFzycDto.getGridType()), TbFzyc::getGridType, tbFzycDto.getGridType())
                .ge(StringUtils.isNotBlank(tbFzycDto.getKssj()), TbFzyc::getRealDate, tbFzycDto.getKssj())
                .le(StringUtils.isNotBlank(tbFzycDto.getJssj()), TbFzyc::getRealDate, tbFzycDto.getJssj())
                .gt(tbFzycDto.getProb() != null, TbFzyc::getProb, tbFzycDto.getProb())
                .orderByDesc(TbFzyc::getRealDate);

        return this.baseMapper.selectPage(new Page<>(tbFzycDto.getPageCurrent(), tbFzycDto.getPageSize()), tb);
    }

    @Override
    public int updateBySfxl(String id) {
        TbFzyc fzyc = this.baseMapper.selectById(id);
        fzyc.setSfxl("1");
        return this.baseMapper.updateById(fzyc);
    }

    @Override
    public Integer getFzyjCount(String ssbmdm, String time, String sfxl) {
        try {
//            ssbmdm = this.tbBaseAdaptRemoteService.getSsbm(ssbmdm, null);
//            time = time.replaceAll("-", "");
//            return this.baseMapper.selectCount(
//                    new LambdaQueryWrapper<TbFzyc>()
//                            .likeRight(!StringUtils.isEmpty(ssbmdm), TbFzyc::getSsbmdm, ssbmdm)
//                            .eq(StringUtils.isNotBlank(sfxl), TbFzyc::getSfxl, sfxl)
//                            .eq(TbFzyc::getRealDate, time)
//            );
            TbFzycDto dto = new TbFzycDto();
            dto.setSsbmdm(ssbmdm);
            dto.setKssj(time);
            dto.setJssj(time);
            dto.setSfxl(sfxl);
            return this.baseMapper.getFzyjCount(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

}
