package com.mvc.service.aiiden;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TReviewCompareMapper;
import com.mvc.entity.TReviewCompare;
import com.mvc.entity.TReviewCompareExample;
import com.mvc.model.*;
import com.mvc.service.taizhang.StationAccService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class TReviewCompareService {

    @Resource
    private TReviewCompareMapper tReviewCompareMapper;

    @Resource
    private TaskReviewService taskReviewService;

    @Resource
    private StationAccService stationAccService;

    public Map<String, Object> findAllTReviewCompare(TReviewCompareModel queryModel) {
        Map<String, Object> resMap = new HashMap<>();
        TReviewCompareExample example=new TReviewCompareExample();
        TReviewCompareExample.Criteria criteria=example.createCriteria();
        if(StringUtils.isNotEmpty(queryModel.getTaskDate())){
            criteria.andTaskDateEqualTo(queryModel.getTaskDate());
        }
        example.setOrderByClause("COMPARE_TYPE");
        Page<Object> pageObject = PageHelper.startPage(queryModel.getPage(), queryModel.getRows());

        List<TReviewCompare> TReviewCompares = tReviewCompareMapper.selectByExample(example);
        List<TReviewCompareModel> resList=new ArrayList<>();
        for(TReviewCompare tReviewCompare:TReviewCompares){
            TReviewCompareModel model = new TReviewCompareModel();
            BeanUtils.copyProperties(tReviewCompare,model);

            StationAccModel accModel = stationAccService.findStationAccByStationCode(tReviewCompare.getStationCode());
            if(accModel!=null){
                if(StringUtils.isNotEmpty(queryModel.getUnitCode())){
                    String unitCode=queryModel.getUnitCode();
                    if(!accModel.getUnitCode().contains(unitCode)&&!accModel.getVisUnitCodes().contains(unitCode)&&
                       !accModel.getCreateUnitCode().contains(unitCode)) continue;//客户登陆时,去掉无权查看的设备
                }
                model.setStationName(accModel.getStationName());
            }
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    /**
     * 获取是否生成审核比对的状态
     * @param taskDate
     * @return 0代表不生成,1代表需要生成
     */
    public String findTReviewCompareStatus(String taskDate) {
        //返回1的原则是,当时所有数据都审核完成了,并且当天没有产生比对数据
        int num = taskReviewService.findDataNumByFinishedFlag(taskDate,"0");//未审核的数量
        if(num!=0) return "0";

        Map<String, Object> resMap = new HashMap<>();
        TReviewCompareExample example=new TReviewCompareExample();
        TReviewCompareExample.Criteria criteria=example.createCriteria();
        criteria.andTaskDateEqualTo(taskDate);

        List<TReviewCompare> tReviewCompares = tReviewCompareMapper.selectByExample(example);
        return tReviewCompares==null||tReviewCompares.size()==0?"1":"0";
    }

    public TReviewCompareModel saveTReviewCompare(TReviewCompareModel model) {
        TReviewCompare TReviewCompare = new TReviewCompare();
        model.setCreateTime(new Date());
        BeanUtils.copyProperties(model,TReviewCompare);
        int insert = tReviewCompareMapper.insert(TReviewCompare);
        return model;
    }

    public int deleteTReviewCompare(int id) {
        int i = tReviewCompareMapper.deleteByPrimaryKey(id);
        return i;
    }
}
