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.oauth.exception.ToflyDeniedException;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.watermeter.custom.entity.meterBusiness.dto.FirstInspectionCountDto;
import com.tofly.watermeter.custom.mapper.meterBusiness.FirstInspectionMapper;
import com.tofly.watermeter.custom.service.meterBusiness.FirstInspectionService;
import com.tofly.watermeter.custom.service.meterBusiness.export.ExportCommonCodeService;
import com.tofly.watermeter.entity.Bwsjdatail;
import com.tofly.watermeter.service.BwsjdatailService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

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

@Service
@AllArgsConstructor
public class FirstInspectionServiceImpl implements FirstInspectionService {

    private final FirstInspectionMapper firstInspectionMapper;

    private final BwsjdatailService bwsjdatailService;

    private final ExportCommonCodeService exportCommonCodeService;

    /**
     * 获得批次信息
     *
     * @param isCheck 是否处理
     * @param cal     口径
     * @return page
     */
    @Override
    public Page getInfo(Page page, String isCheck, Long cal) {
        return firstInspectionMapper.getBatchInfo(page, isCheck, cal);
    }

    @Override
    public List<Map> count(HttpServletResponse response, FirstInspectionCountDto dto) {
        List<Map> listInfo = firstInspectionMapper.count(dto);
        if (dto.getExport() == 1) {
            exportCommonCodeService.exportCode(response, "firstCheckCountService", listInfo);
        }
        return listInfo;
    }

    /**
     * 首检
     *
     * @param bwsjdatail bwsjdatail
     * @return boolean
     */
    @Override
    public boolean firstCheck(Bwsjdatail bwsjdatail) {
        if (bwsjdatail == null) {
            throw new ToflyDeniedException("传入数据为空");
        }
        if (bwsjdatail.getId() == null) {
            throw new ToflyDeniedException("传入id为空");
        }
        return bwsjdatailService.update(Wrappers.<Bwsjdatail>lambdaUpdate()
                .eq(Bwsjdatail::getId, bwsjdatail.getId())
                .set(Bwsjdatail::getIsCheck, 1)
                .set(Bwsjdatail::getBwBhgs,bwsjdatail.getBwBhgs())
                .set(Bwsjdatail::getBwBhgs, bwsjdatail.getBwBhgs())
                .set(Bwsjdatail::getWbbm, bwsjdatail.getWbbm())
                .set(Bwsjdatail::getBwBz, bwsjdatail.getBwBz())
                .set(Bwsjdatail::getBwJybm, bwsjdatail.getBwJybm())
                .set(Bwsjdatail::getBwJyrq, new Date())
                .set(Bwsjdatail::getBwHgsl, bwsjdatail.getBwHgsl())
                .set(Bwsjdatail::getBwHgbl, String.format("%.2f", (bwsjdatail.getBwHgsl() / bwsjdatail.getBwCjsl()) * 100D))
                .set(Bwsjdatail::getOperateDate, new Date())
                .set(Bwsjdatail::getOperateUser, SecurityUtils.getUserId()));
    }

    /**
     * 第二层，查看批次信息
     *
     * @param dto 条件
     * @return page
     */
    @Override
    public Page<Map> countByCal(HttpServletResponse response, Page page, FirstInspectionCountDto dto) {

        if (StringUtils.isEmpty(dto.getState())) {
            throw new ToflyDeniedException("状态不允许为空");
        }

        Page<Map> result;

        if (dto.getState() == 1) {
            result = firstInspectionMapper.queryInventory(page, dto);
        } else {
            result = firstInspectionMapper.queryFirstCheck(page, dto);
        }

        if (dto.getExport() == 1) {

            List<Map> listInfo;
            String service;

            if (dto.getState() == 1) {
                //导出
                listInfo = firstInspectionMapper.queryInventory(dto);
                service = "queryInventoryService";
            } else {
                //导出
                listInfo = firstInspectionMapper.queryFirstCheck(dto);
                service = "queryFirstCheckService";
            }

            exportCommonCodeService.exportCode(response, service, listInfo);
        }
        return result;
    }
}
