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.watermeter.config.FilePathBean;
import com.tofly.watermeter.custom.entity.meterBusiness.dto.FaultSpotCheckConditionDto;
import com.tofly.watermeter.custom.mapper.meterBusiness.FaultSpotCheckMapper;
import com.tofly.watermeter.custom.service.ReadDataCheckThreeService;
import com.tofly.watermeter.custom.service.meterBusiness.FaultSpotCheckService;
import com.tofly.watermeter.custom.service.meterBusiness.export.ExportCommonCodeService;
import com.tofly.watermeter.custom.vo.BwgzbcjdatailVO;
import com.tofly.watermeter.entity.Bwgzbcjdatail;
import com.tofly.watermeter.entity.Bwsbyjgl;
import com.tofly.watermeter.service.BwgzbcjdatailService;
import com.tofly.watermeter.service.BwsbyjglService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

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

@Service
public class FaultSpotCheckServiceImpl implements FaultSpotCheckService {

    @Autowired
    private FaultSpotCheckMapper faultSpotCheckMapper;
    @Autowired
    private ExportCommonCodeService commonCodeService;
    @Autowired
    private ReadDataCheckThreeService readDataCheckThreeService;
    @Autowired
    private FilePathBean filePathBean;
    @Autowired
    private BwsbyjglService bwsbyjglService;
    @Autowired
    private BwgzbcjdatailService bwgzbcjdatailService;

    private final static String FAULT_TABLE_NAME = "TF_YWPN_BWGZBCJDATAIL_W";

    @Override
    public List<Map> count(HttpServletResponse response, FaultSpotCheckConditionDto dto) {
        List<Map> count = faultSpotCheckMapper.count(dto);

        if (dto.getExport() == 1) {
            commonCodeService.exportCode(response, "fscCountService", count);
        }
        return count;
    }

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

        List<Map> list = null;

        String service = "";

        if (dto.getInfo() == 0) {
            result = faultSpotCheckMapper.queryInfo(page, dto);
            if (dto.getExport() == 1) {
                list = faultSpotCheckMapper.queryInfo(dto);
                service = "fscQueryService";
            }
        } else {
            result = faultSpotCheckMapper.queryWeekInfo(page, dto);
            if (dto.getExport() == 1) {
                list = faultSpotCheckMapper.queryWeekInfo(dto);
                service = "fscQueryFaultService";
            }
        }
        if (dto.getExport() == 1) {
            commonCodeService.exportCode(response, service, list);
        }

        return result;
    }

    @Override
    public boolean insertFault(List<MultipartFile> fileList, BwgzbcjdatailVO tail) {
        if (fileList != null && fileList.size() != 0) {
            StringBuilder stringBuilder = readDataCheckThreeService.saveFileList(fileList, filePathBean.getFaultPath(), FAULT_TABLE_NAME);
            tail.setBwFileid(stringBuilder.toString());
        }

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

        if (bwsbyjgl == null) {
            throw new ToflyDeniedException("该水表不是故障移交水表");
        }

        tail.setBwLsbh(System.currentTimeMillis() + "");
        tail.setBwJyrq(new Date());

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

    }
}
