package com.tofly.watermeter.custom.service.meterBusiness.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.common.core.util.DateUtil;
import com.tofly.common.oauth.exception.ToflyDeniedException;
import com.tofly.watermeter.custom.entity.meterBusiness.dto.WeekSpotCheckConditionDto;
import com.tofly.watermeter.custom.mapper.meterBusiness.WeekSpotCheckMapper;
import com.tofly.watermeter.custom.service.meterBusiness.WeekSpotCheckService;
import com.tofly.watermeter.custom.service.meterBusiness.export.ExportCommonCodeService;
import com.tofly.watermeter.custom.vo.BwzjbcjdatailVO;
import com.tofly.watermeter.entity.Bwsbyjgl;
import com.tofly.watermeter.entity.Bwzjbcjdatail;
import com.tofly.watermeter.service.BwsbyjglService;
import com.tofly.watermeter.service.BwzjbcjdatailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WeekSpotCheckServiceImpl implements WeekSpotCheckService {

    @Autowired
    private WeekSpotCheckMapper weekSpotCheckMapper;
    @Autowired
    private ExportCommonCodeService exportCommonCodeService;
    @Autowired
    private BwsbyjglService bwsbyjglService;
    @Autowired
    private BwzjbcjdatailService bwzjbcjdatailService;

    @Override
    public List<Map> count(HttpServletResponse response, WeekSpotCheckConditionDto dto) {
        List<Map> result = weekSpotCheckMapper.count(dto);
        if (dto.getExport() == 1) {
            exportCommonCodeService.exportCode(response, "weekSpotCheckCountService", result);
        }
        return result;
    }

    @Override
    public Page<Map> queryInfo(Page page, HttpServletResponse response, WeekSpotCheckConditionDto dto) {

        Page<Map> result;

        List<Map> list = null;

        String service = "";

        if (dto.getInfo() == 0) {
            result = weekSpotCheckMapper.queryInfo(page, dto);
            if (dto.getExport() == 1) {
                list = weekSpotCheckMapper.queryInfo(dto);
                service = "wscQueryInfoService";
            }
        } else {
            result = weekSpotCheckMapper.queryWeekInfo(page, dto);
            if (dto.getExport() == 1) {
                list = weekSpotCheckMapper.queryWeekInfo(dto);
                service = "wscQueryWeekService";
            }
        }
        if (dto.getExport() == 1) {
            exportCommonCodeService.exportCode(response, service, list);
        }

        return result;
    }

    @Override
    public boolean insertWeek(BwzjbcjdatailVO tail) {
        if (tail == null) {
            throw new ToflyDeniedException("传入数据为空");
        }
        Bwsbyjgl bwsbyjgl = bwsbyjglService.getOne(Wrappers.<Bwsbyjgl>lambdaQuery()
                .eq(Bwsbyjgl::getSbzsbh, tail.getBwSbbh())
                .eq(Bwsbyjgl::getBwYjlx, 3)
                .eq(Bwsbyjgl::getBwLsbh, tail.getLsbh())
        );

        if (bwsbyjgl == null) {
            throw new ToflyDeniedException("该水表不是周检移交水表");
        }
        String q1 = tail.getBwQ1();
        String q2 = tail.getBwQ2();
        String q3 = tail.getBwQ3();
        tail.setBwLsbh(DateUtil.getDateFormat(new Date(), "yyyyMMdd-") + System.currentTimeMillis());
        String divide = (new BigDecimal(q1).add(new BigDecimal(q2)).add(new BigDecimal(q3))).divide(new BigDecimal("3"), 4).toString();
        tail.setBwPjz(divide);
        tail.setBwJyrq(new Date());

        bwzjbcjdatailService.save(tail);
        return bwsbyjglService.update(Wrappers.<Bwsbyjgl>lambdaUpdate()
                .eq(Bwsbyjgl::getSbzsbh, tail.getBwSbbh())
                .set(Bwsbyjgl::getCheckFlag, 1));
    }

    @Override
    public Page<Map> getTurnOverTable(Page page, Long type, Long vendor, Long cal, Long tableType, String dto, String lsbh) {
        return weekSpotCheckMapper.getTurnOverTable(page, type, vendor, cal, tableType, dto, lsbh);
    }

    @Override
    public Map<String, Long> getCheckNum(String lsbh, Long cal, Long yjlx, String tableName) {
        return weekSpotCheckMapper.getCheckNum(lsbh, cal, yjlx, tableName);
    }


}
