package com.tofly.feesapi.yhgl.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.feesapi.bjgl.entity.YhCbkxx;
import com.tofly.feesapi.bmgl.entity.BmSj;
import com.tofly.feesapi.bmgl.entity.BmYslx;
import com.tofly.feesapi.bmgl.service.BmSjService;
import com.tofly.feesapi.bmgl.service.BmYslxService;
import com.tofly.feesapi.common.core.dto.PageDto;
import com.tofly.feesapi.common.core.vo.PageResult;
import com.tofly.feesapi.common.util.GuidGenerator;
import com.tofly.feesapi.security.util.SecurityUtils;
import com.tofly.feesapi.yhgl.entity.YhKhjbxx;
import com.tofly.feesapi.yhgl.entity.YhXzbgLog;
import com.tofly.feesapi.yhgl.entity.dto.YhXzbgLogFilter;
import com.tofly.feesapi.yhgl.entity.vo.YhXzbgLogSelect;
import com.tofly.feesapi.yhgl.mapper.YhXzbgLogMapper;
import com.tofly.feesapi.yhgl.service.YhKhjbxxService;
import com.tofly.feesapi.yhgl.service.YhXzbgLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author sunjian
 * @date 2025/2/11
 */
@Service("yhXzbgLogService")
public class YhXzbgLogServiceImpl extends ServiceImpl<YhXzbgLogMapper, YhXzbgLog> implements YhXzbgLogService {

    @Autowired
    private YhKhjbxxService yhKhjbxxService;
    @Autowired
    private BmSjService bmSjService;
    @Autowired
    private BmYslxService bmYslxService;

    @Override
    public PageResult<YhXzbgLogSelect> getYhxzbgLogtj(PageDto pageDto, YhXzbgLogFilter filter) {
        PageDto<YhXzbgLogSelect> page = baseMapper.getYhxzbgLogtj(pageDto, filter);
        return page.toVo();
    }

    @Override
    public List<YhXzbgLogSelect> yhxzbgLogtjlist(YhXzbgLogFilter filter) {
        return this.baseMapper.getYhxzbgLogtj(filter);
    }

    @Override
    public boolean saveYhxzbgLog(YhCbkxx oldYhCbkxx, YhCbkxx newYhCbkxx) {
        YhKhjbxx yhKhjbxx = yhKhjbxxService.getById(newYhCbkxx.getYhbh());
        YhXzbgLog yhxzbgLog = new YhXzbgLog();
        yhxzbgLog.setBh(GuidGenerator.getGuid().substring(0, 8));
        yhxzbgLog.setYhxm(yhKhjbxx.getYhxm());
        yhxzbgLog.setYhdz(yhKhjbxx.getYhdz());
        yhxzbgLog.setLxdh(yhKhjbxx.getLxdh());
        yhxzbgLog.setSsgs(yhKhjbxx.getSsgs());
        yhxzbgLog.setSscbpq(yhKhjbxx.getSscbpq());
        yhxzbgLog.setSscbb(yhKhjbxx.getSscbb());
        yhxzbgLog.setGdzt("2");
        yhxzbgLog.setBz("水价批量修改");
        List<BmSj> bmSjList = bmSjService.list();
        List<BmYslx> bmYslxList = bmYslxService.list();

        BmSj bmSj = bmSjList.stream().filter(e -> e.getCode().equals(oldYhCbkxx.getSjbm())).findAny().get();

        yhxzbgLog.setOldYbdjbz(oldYhCbkxx.getYbdjbz());
        yhxzbgLog.setOldYslx(bmSj.getYslx());
        yhxzbgLog.setOldYslxmc(bmYslxList.stream().filter(bmYslx -> bmYslx.getCode().equals(yhxzbgLog.getOldYslx())).findFirst().get().getName());
        yhxzbgLog.setOldSjbm(oldYhCbkxx.getSjbm());
        yhxzbgLog.setOldSjmc(bmSj.getName());
        yhxzbgLog.setOldSj(this.getSjstr(bmSj));
        if (StringUtil.isNotEmpty(oldYhCbkxx.getSjbm1())) {
            bmSj = bmSjList.stream().filter(e -> e.getCode().equals(oldYhCbkxx.getSjbm1())).findFirst().get();
            yhxzbgLog.setOldSjbm1(oldYhCbkxx.getSjbm1());
            yhxzbgLog.setOldSjmc1(bmSj.getName());
            yhxzbgLog.setOldSj(this.getSjstr(bmSj));
        }
        if (StringUtil.isNotEmpty(oldYhCbkxx.getSjbm2())) {
            bmSj = bmSjList.stream().filter(e -> e.getCode().equals(oldYhCbkxx.getSjbm2())).findFirst().get();
            yhxzbgLog.setOldSjbm2(oldYhCbkxx.getSjbm2());
            yhxzbgLog.setOldSjmc2(bmSj.getName());
            yhxzbgLog.setOldSj(this.getSjstr(bmSj));
        }
        bmSj = bmSjList.stream().filter(e -> e.getCode().equals(newYhCbkxx.getSjbm())).findFirst().get();
        yhxzbgLog.setNewYbdjbz(newYhCbkxx.getYbdjbz());
        yhxzbgLog.setNewYslx(bmSj.getYslx());
        yhxzbgLog.setNewYslxmc(bmYslxList.stream().filter(bmYslx -> bmYslx.getCode().equals(yhxzbgLog.getNewYslx())).findFirst().get().getName());
        yhxzbgLog.setNewSjmc(bmSj.getName());
        yhxzbgLog.setNewSjbm(newYhCbkxx.getSjbm());
        yhxzbgLog.setNewSj(this.getSjstr(bmSj));
        if (StringUtil.isNotNull(newYhCbkxx.getSlbl()) && newYhCbkxx.getSlbl().compareTo(BigDecimal.ZERO) > 0) {
            yhxzbgLog.setNewSlbl(newYhCbkxx.getSlbl());
        }
        if (StringUtil.isNotEmpty(newYhCbkxx.getSjbm1())) {
            bmSj = bmSjList.stream().filter(e -> e.getCode().equals(newYhCbkxx.getSjbm1())).findFirst().get();
            yhxzbgLog.setNewSjmc1(bmSj.getName());
            yhxzbgLog.setNewSjbm1(newYhCbkxx.getSjbm1());
            yhxzbgLog.setNewSj1(this.getSjstr(bmSj));
            yhxzbgLog.setNewSlbl1(newYhCbkxx.getSlbl1());
        }
        if (StringUtil.isNotEmpty(newYhCbkxx.getSjbm2())) {
            bmSj = bmSjList.stream().filter(e -> e.getCode().equals(newYhCbkxx.getSjbm2())).findFirst().get();
            yhxzbgLog.setNewSjmc2(bmSj.getName());
            yhxzbgLog.setNewSjbm2(newYhCbkxx.getSjbm2());
            yhxzbgLog.setNewSj2(this.getSjstr(bmSj));
            yhxzbgLog.setNewSlbl2(newYhCbkxx.getSlbl2());
        }
        yhxzbgLog.setCzrybm(String.valueOf(SecurityUtils.getUserId()));
        yhxzbgLog.setCzryxm(SecurityUtils.getUser().getRealName());
        yhxzbgLog.setBgfx(getBgfx(
                bmSjList.stream().filter(e -> e.getCode().equals(oldYhCbkxx.getSjbm())).findFirst().get(),
                bmSjList.stream().filter(e -> e.getCode().equals(newYhCbkxx.getSjbm())).findFirst().get()));
        yhxzbgLog.setBgnr(String.format("%s变更为%s", yhxzbgLog.getOldYslxmc(), yhxzbgLog.getNewYslxmc()));
        yhxzbgLog.setBgsj(new Date());
        return this.save(yhxzbgLog);
    }


    private String getSjstr(BmSj bmSj) {
        String sjstr = String.format("单价：%s元/方", bmSj.getSj());
        if (bmSj.getJtbz().equals("1")) {
            sjstr = String.format("一阶：%s元/方；二阶：%s元/方；三阶：%s元/方", bmSj.getJt1Sj(), bmSj.getJt2Sj(), bmSj.getJt3Sj());
        }
        return sjstr;
    }

    private String getBgfx(BmSj oldbmSj, BmSj newbmSj) {
        BigDecimal oldSj = oldbmSj.getSj();
        BigDecimal newSj = newbmSj.getSj();
        if (oldbmSj.getJtbz().equals("1")) {
            oldSj = oldbmSj.getJt1Sj();
        }
        if (newbmSj.getJtbz().equals("1")) {
            newSj = newbmSj.getJt1Sj();
        }
        if (oldSj.compareTo(newSj) == 0) {
            return "0";
        }
        if (oldSj.compareTo(newSj) > 0) {
            return "2";
        }
        return "1";
    }
}
