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

import com.chinaitop.depot.intelligent.basicdata.model.TKtinfo;
import com.chinaitop.depot.intelligent.basicdata.service.TKtinfoService;
import com.chinaitop.depot.intelligent.coldStorage.mapper.TKtinfoHistoryMapper;
import com.chinaitop.depot.intelligent.coldStorage.model.TKtinfoHistory;
import com.chinaitop.depot.intelligent.coldStorage.model.TKtinfoHistoryExample;
import com.chinaitop.depot.intelligent.coldStorage.service.ColdStorageService;
import com.chinaitop.depot.intelligent.insideCirculation.service.InsideCirculationService;
import com.chinaitop.depot.intelligent.utils.JsonToObjectUtils;
import com.chinaitop.depot.intelligent.utils.UuidUtils;
import com.chinaitop.depot.intelligent.ventilation.mapper.TCtldevinfoMapper;
import com.chinaitop.depot.intelligent.ventilation.service.AerationParameterService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author qingsong.han
 * @description: 低温储粮
 * @create 2019-01-25 10:52
 */
@Service
public class InsideCirculationServiceImpl implements InsideCirculationService {
    
    @Autowired
    private TKtinfoHistoryMapper ktinfoHistoryMapper;
    @Resource
    private AerationParameterService aerationParameterService;
    
    /**
     * 空调作业记录条件列表
     * @param orgId
     * @param vcfcode
     * @return
     */
    @Override
    public List<TKtinfoHistory> getList(String orgId, String vcfcode) {
        TKtinfoHistoryExample example = new TKtinfoHistoryExample();
        TKtinfoHistoryExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(orgId))
            criteria.andOrgIdEqualTo(orgId);
        if (StringUtils.isNotBlank(vcfcode))
            criteria.andVcfcodeEqualTo(vcfcode);
        
        int a = 0;
        BigDecimal b = new BigDecimal(a);
        criteria.andEnergyEqualTo(b);
        example.setOrderByClause("starttime DESC");
        return ktinfoHistoryMapper.selectByExample(example);
    	
    }

    /**
     * 保存空调作业记录
     * @param type
     * @param ktinfoJson
     * @param uperson
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveHistoryData(String type, String ktinfoJson, String uperson,Integer orgId) {
        TKtinfo tKtinfo = (TKtinfo) JsonToObjectUtils.jsonToObject(ktinfoJson, TKtinfo.class);
        TKtinfoHistory ktinfoHistory = new TKtinfoHistory();
        int a = 0;
        BigDecimal b = new BigDecimal(a);
        if ("open".equals(type)) {
            ktinfoHistory.setId(UuidUtils.getCode()); // id
            ktinfoHistory.setVcfcode(tKtinfo.getVcfcode()); // 仓房code
            ktinfoHistory.setVdevcode(tKtinfo.getVdevcode()); // 设备code
            ktinfoHistory.setVdevname(tKtinfo.getVdevname()); // 设备名称
            ktinfoHistory.setUperson(uperson); // 操作人
            ktinfoHistory.setRemark(tKtinfo.getRemark()); // 具体详细信息
            ktinfoHistory.setOrgId(tKtinfo.getOrgId()); // 组织机构代码
            ktinfoHistory.setStarttime(new Date()); // 开始时间
            ktinfoHistory.setEnergy(b); //区分是空调还是内环流
            ktinfoHistoryMapper.insertSelective(ktinfoHistory); // 新增作业记录
            aerationParameterService.save(ktinfoJson,orgId); // 更新操作状态
        } else if ("close".equals(type)){
            TKtinfoHistoryExample example = new TKtinfoHistoryExample();
            TKtinfoHistoryExample.Criteria criteria = example.createCriteria();
            criteria.andOrgIdEqualTo(tKtinfo.getOrgId());
            criteria.andVcfcodeEqualTo(tKtinfo.getVcfcode());
            criteria.andVdevcodeEqualTo(tKtinfo.getVdevcode());
            criteria.andVdevnameEqualTo(tKtinfo.getVdevname());
            criteria.andEndtimeIsNull();
            ktinfoHistory.setEnergy(b); //区分是空调还是内环流
            ktinfoHistory.setEndtime(new Date()); // 结束时间
            ktinfoHistoryMapper.updateByExampleSelective(ktinfoHistory,example); // 更新作业记录
            aerationParameterService.save(ktinfoJson,orgId); // 更新操作状态
        }
    }

}
