package com.tofly.third.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.third.common.exception.BusinessException;
import com.tofly.third.common.req.Cbxx;
import com.tofly.third.common.util.ServerUtil;
import com.tofly.third.entity.YhCbkxx;
import com.tofly.third.entity.YhKhjbxx;
import com.tofly.third.entity.ZnbCbLog;
import com.tofly.third.mapper.ZnbCbLogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

@Slf4j
@Service
public class ZnbCbLogService extends ServiceImpl<ZnbCbLogMapper, ZnbCbLog> {

    @Autowired
    private SysGdcsbService gdcsbService;

    public ZnbCbLog znbcb(YhKhjbxx yhKhjbxx, YhCbkxx cbkxx, Cbxx cbxx) {
        ZnbCbLog znbCbLog = new ZnbCbLog();
        znbCbLog.setYhbh(yhKhjbxx.getYhbh())
                .setCbkh(cbkxx.getCbkh())
                .setCbkid(cbkxx.getCbkid())
                .setBjbh(cbkxx.getSbbh())
                .setSccj(cbkxx.getSbsccj())
                .setSsgs("1")
                .setPch(ServerUtil.getZnbPCH())
                .setCzry("4")
                .setCzsj(LocalDateTime.now())
                .setClLock("0")
                .setClCljg("0")
                .setClClry("系统机器");

        znbCbLog.setSscbpq(yhKhjbxx.getSscbpq())
                .setSscbb(yhKhjbxx.getSscbb());

        String cwrq = gdcsbService.selectCwrq();
        znbCbLog.setCwrq(BigDecimal.valueOf(Long.parseLong(cwrq)));

        ZnbCbLog scyccbLog = getZxCbLogByCbkId(cbkxx.getCbkid());
        if (ObjectUtil.isNull(scyccbLog)) {
            znbCbLog.setSccbrq(null).setScds(BigDecimal.valueOf(cbkxx.getSbqs()));
        } else {
            if(ObjectUtil.isNotNull(scyccbLog.getCbrq())) {
                znbCbLog.setSccbrq(scyccbLog.getCbrq());
            }
            znbCbLog.setScds(scyccbLog.getBcds());
        }

        // TODO: 2025/9/2 水表读数 暂定向下取整，不保留小数位
        BigDecimal cbzs = new BigDecimal(cbxx.getCbzs());
        BigDecimal result = cbzs.setScale(0, RoundingMode.FLOOR);

        BigDecimal cbsl = result.subtract(znbCbLog.getScds());

        znbCbLog.setCbZs(result)
                .setCbSl(cbsl)
                .setBcds(result)
                .setCbrybm("4")
                .setCbryxm("物联网抄表");

        String cbsj = cbxx.getCbsj();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
        LocalDateTime cbrq = LocalDateTime.parse(cbsj, formatter);
        znbCbLog.setCbrq(cbrq)
                .setLrrybm("3")
                .setLrryxm("系统机器")
                .setLrrq(new Date())
                .setCbpl(6);

        znbCbLog.setClClsj(LocalDateTime.now())
                .setClCljg("1");

        if ("1".equals(cbxx.getBjbhpp())) {
            znbCbLog.setCbbz("水表厂家推送的表具编号与水表档案的不匹配！");
        }
        save(znbCbLog);
        return znbCbLog;
    }

    public ZnbCbLog getZxCbLogByCbkId(String cbkId) {
        if (StrUtil.isEmpty(cbkId)) {
            throw new BusinessException("未找到抄表卡信息");
        }

        return baseMapper.getZxCbLogByCbkId(cbkId);
    }

}
