package com.chinaitop.depot.intelligent.fumigation.service.impl;

import com.chinaitop.depot.intelligent.energyMonitor.mapper.TEnergyInfoMapper;
import com.chinaitop.depot.intelligent.energyMonitor.model.TEnergyInfo;
import com.chinaitop.depot.intelligent.energyMonitor.model.TEnergyInfoExample;
import com.chinaitop.depot.intelligent.energyMonitor.service.EnergyDetectionService;
import com.chinaitop.depot.intelligent.fumigation.mapper.TFumigationPesticideMapper;
import com.chinaitop.depot.intelligent.fumigation.mapper.TFumigationPlanMapper;
import com.chinaitop.depot.intelligent.fumigation.mapper.TFumigationProcessMapper;
import com.chinaitop.depot.intelligent.fumigation.model.*;
import com.chinaitop.depot.intelligent.fumigation.service.FumigationPlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lvzhikai
 * @description: 熏蒸管理
 * @create 2018-12-15 11:00
 */
@Service
public class FumigationPlanServiceImpl implements FumigationPlanService {
    @Resource
    private TFumigationProcessMapper tFumigationProcessMapper;

    @Resource
    private TFumigationPlanMapper tFumigationPlanMapper;

    @Resource
    private TFumigationPesticideMapper tFumigationPesticideMapper;

    /**
     * 列表
     * @param example
     * @return
     */
    @Override
    public List<TFumigationPlan> getFumigationPlanList(TFumigationPlanExample example) {
        return tFumigationPlanMapper.selectByExample(example);
    }

    @Override
    public List<TFumigationPlan> getProcessList(Map<String, Object> map) {
        return tFumigationPlanMapper.getProcessList(map);
    }

    @Override
    public TFumigationPlan findById(String fumigationId) {
        return tFumigationPlanMapper.selectByPrimaryKey(fumigationId);
    }

    @Override
    public void add(TFumigationPlan tFumigationPlan) {
        tFumigationPlanMapper.insert(tFumigationPlan);
    }

    @Override
    public void update(TFumigationPlan tFumigationPlan) {
        tFumigationPlanMapper.updateByPrimaryKeySelective(tFumigationPlan);
    }

    @Override
    public void updateByExample(TFumigationPlan tFumigationPlan, TFumigationPlanExample example) {
        tFumigationPlanMapper.updateByExampleSelective(tFumigationPlan, example);
    }

    /**
     * 获取方案列表
     * @param map
     * @return
     */
    @Override
    public List<Map<String,Object>> selectPlanQueryList(Map<String, Object> map) {
        return tFumigationPlanMapper.selectPlanQueryList(map);
    }

    /**
     * 作业全部信息--省级平台用
     * @param map
     * @return
     */
    @Override
    public List<Map<String,Object>> getAllQueryList(Map<String, Object> map) {
        return tFumigationPlanMapper.getAllQueryList(map);
    }

    /**
     * 获取熏蒸审批方案列表
     * @param map
     * @return
     */
    @Override
    public List<Map<String,Object>> getFumigationApproveList(Map<String, Object> map) {
        return tFumigationPlanMapper.getFumigationApproveList(map);
    }

    @Transactional
    @Override
    public void insertBatch(List<TFumigationProcess> fumigations) {
        tFumigationProcessMapper.insertBatch(fumigations);
    }

    @Override
    public List<TFumigationProcess> getByCodeAndTimes(List<String> cDcsCodes, List<Date> cTimes) {
        TFumigationProcessExample example = new TFumigationProcessExample();
        TFumigationProcessExample.Criteria criteria = example.createCriteria();
        if ((cDcsCodes != null && cDcsCodes.size() > 0) && (cTimes != null && cTimes.size() > 0)) {
            criteria.andFumigationIdIn(cDcsCodes);
            criteria.andCreateTimeIn(cTimes);
            List<TFumigationProcess> tFumigationProcesses = tFumigationProcessMapper.selectByExample(example);
            return tFumigationProcesses;
        }
        return null;
    }

	@Override
	public List<Map<Object, Object>> getJcd(Map<Object, Object> map) {
		return tFumigationPlanMapper.getJcd(map);
	}
	
	/**
     * 提交
     */
    @Override
    public void submit(String id, Integer approvalStatus) {
        //熏蒸修改审批状态
        TFumigationPlan tFumigationPlan = tFumigationPlanMapper.selectByPrimaryKey(id);
        if (tFumigationPlan == null) {
            throw new RuntimeException("没有找到相应的数据");
        }

        // 设置审批状态为，保管科长审批中.
        tFumigationPlan.setState(approvalStatus);
        tFumigationPlan.setApproveTime(new Date());
        tFumigationPlanMapper.updateByPrimaryKey(tFumigationPlan);

        //施药修改审批状态
        TFumigationPesticideExample example = new TFumigationPesticideExample();
        TFumigationPesticideExample.Criteria criteria = example.createCriteria();
        criteria.andFumigationIdEqualTo(id);
        List<TFumigationPesticide> list = tFumigationPesticideMapper.selectByExample(example);

        for (TFumigationPesticide tfp:list) {
            tfp.setState(approvalStatus);
            tfp.setApproveTime(new Date());
            // 修改.
            tFumigationPesticideMapper.updateByPrimaryKey(list.get(0));
        }
    }
}
