/*
 * Author: wangjian
 * 生成ServiceImpl
 */
package com.tofly.watermeter.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.watermeter.custom.dto.ChangeTableDto;
import com.tofly.watermeter.custom.entity.Statistic;
import com.tofly.watermeter.custom.entity.file.TurnoverFileInfo;
import com.tofly.watermeter.entity.Bwhbdatail;
import com.tofly.watermeter.entity.Qfkhjbxx;
import com.tofly.watermeter.mapper.BwhbdatailMapper;
import com.tofly.watermeter.service.BwhbdatailService;
import com.tofly.watermeter.service.QfkhjbxxService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 水表换表记录表
 *
 * @author admin
 * @date Wed Mar 10 00:00:00 CST 2021
 */
@Service("bwhbdatailService")
public class BwhbdatailServiceImpl extends ServiceImpl<BwhbdatailMapper, Bwhbdatail> implements BwhbdatailService {

    @Autowired
    private  QfkhjbxxService qfkhjbxxService;

    @Override
    public IPage getExchange(Page page, String inputcontent, String sbbh, Date startTime, Date endTime) {
        return baseMapper.getExchange(page, inputcontent, sbbh, startTime, endTime);
    }

    @Override
    public IPage<Map> getSyDate(Page page, Statistic statistic) {
        Integer syDate = statistic.getSyDate();
        List<String> cjList = null;
        List<String> kjList = null;
        List<String> lxList = null;
        String bwSbcj = statistic.getSbcj();
        String bwSbkj = statistic.getSbkj();
        String bwSblx = statistic.getSblx();
        if (StringUtil.isNotEmpty(bwSbcj)) {
            cjList = Arrays.asList(bwSbcj.split(","));
        }
        if (StringUtil.isNotEmpty(bwSbkj)) {
            kjList = Arrays.asList(bwSbkj.split(","));
        }
        if (StringUtil.isNotEmpty(bwSblx)) {
            lxList = Arrays.asList(bwSblx.split(","));
        }
        return baseMapper.getSyDate(page, cjList, kjList, lxList, syDate);
    }

    @Override
    public IPage<Map> getSyDateDetails(Page page, Long cj, Long kj, Long lx, Integer syDate) {
        return baseMapper.getSyDateDetails(page, cj, kj, lx, syDate);
    }

    @Override
    public List<Map> getSyDateDetailsList(Long cj, Long kj, Long lx, Integer syDate) {
        return baseMapper.getSyDateDetailsList(cj, kj, lx, syDate);
    }

    @Override
    public List<Bwhbdatail> getYjInfo(List<TurnoverFileInfo> turnoverList) {
        return baseMapper.getYjInfo(turnoverList);
    }

    @Override
    public Object saveChangeTable(ChangeTableDto dto) {
        Bwhbdatail bwhbdatail = new Bwhbdatail();
        BeanUtils.copyProperties(dto,bwhbdatail);
        long betweenDay = DateUtil.between(dto.getJbAzrq(), dto.getBwHbrq(), DateUnit.DAY);
        bwhbdatail.setSyDate(betweenDay);
        Qfkhjbxx qfkhjbxx = qfkhjbxxService.getOne(new QueryWrapper<Qfkhjbxx>().lambda().eq(Qfkhjbxx::getYhbh,dto.getBwKhbh()));
        if(qfkhjbxx!=null){
            bwhbdatail.setBwKhxm(qfkhjbxx.getYhxm());
            bwhbdatail.setBwKhdz(qfkhjbxx.getYhdz());
            bwhbdatail.setBwKhdh(qfkhjbxx.getYhbh());
        }
        return this.save(bwhbdatail);
    }

}
