package com.unis.detectionnum.server.domain;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.unis.common.utils.LsBeanUtils;
import com.unis.detectionnum.common.vo.ScanRuleVO;
import com.unis.detectionnum.server.dao.DCfxxsjyDao;
import com.unis.detectionnum.server.dao.ScanRuleDao;
import com.unis.detectionnum.server.dao.TaskInfoDao;
import com.unis.detectionnum.server.entity.ScanRuleDO;
import com.unis.detectionnum.server.entity.TaskInfoDO;

@Component
public class ScanRuleDomain extends BaseDomain {

    @Autowired
    private ScanRuleDao scanRuleDao;

    @Autowired
    private DCfxxsjyDao cfxxsjyDao;

    @Autowired
    private TaskInfoDao taskInfoDao;

    /**
     * @param @param  scanRuleVO
     * @param @return
     * @return int
     * @throws
     * @Title: saveOrUpdateScanRule
     * @Description: TODO
     */
    @Transactional
    public int saveOrUpdateScanRule(ScanRuleVO scanRuleVO) {

        ScanRuleDO scanRuleDO = new ScanRuleDO();
        LsBeanUtils.copyAllProperties(scanRuleVO, scanRuleDO);

        String sbCode = scanRuleVO.getSbCode();
        //根据设备code获取扫描规则，若存在记录则修改
        ScanRuleDO ruleDO = scanRuleDao.selectBySbCode(sbCode);
        if (scanRuleVO.getIsOut().equals("0") || scanRuleVO.getIsOut() == 0) {
            scanRuleDO.setEndHour("");
            scanRuleDO.setStartHour("");
            scanRuleDO.setIntervalHour(0);
            if (ruleDO != null) {
                scanRuleDao.updateBySbCodeKeySelective(scanRuleDO);
            } else {
                //若不存在记录则新增
                scanRuleDao.insertSelective(scanRuleDO);
            }
        } else {
            scanRuleDO.setInventoryHour(0);
            if (ruleDO != null) {
                scanRuleDao.updateBySbCodeKeySelective(scanRuleDO);
            } else {
                //若不存在记录则新增
                scanRuleDao.insertSelective(scanRuleDO);
            }
        }
        //探头默认扫描时间
        String cronExpr = "0 0 12 * * ?";

        //根据规则状态修改定时表达式  1、为库存时间
        if (scanRuleDO.getIsOut() == 0) {
            Integer inventoryHour = 12;
            if (scanRuleDO.getInventoryHour() != null) {
                inventoryHour = scanRuleDO.getInventoryHour();
            }
            cronExpr = "0 0 " + inventoryHour + " * * ?";
            //根据规则状态修改定时表达式  2、出库时间
        } else if (scanRuleDO.getIsOut() == 1) {
            //已知前端对开始时间与结束时间做相关校验
            //int start = Integer.parseInt(scanRuleDO.getStartHour());
            //int end = Integer.parseInt(scanRuleDO.getEndHour());
            int interval = 2;
            if (scanRuleDO.getIntervalHour() != null) {
                interval = scanRuleDO.getIntervalHour();
            }
	      /*  int minute = interval * 60;
	        
			String subCronExpr = "";
			SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm");
	        Calendar calendar = Calendar.getInstance();
	        try {
				calendar.setTime(simpleDateFormat.parse(scanRuleDO.getStartHour()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
	        //时间转换后minute就是间隔时间(小时为单位转换为分钟为单位)
	        calendar.add(Calendar.MINUTE, minute);
	        Date result=calendar.getTime();
	        String res=simpleDateFormat.format(result);
	        //所有关于时间的值转换并计算完毕后进行数据格式拼接给定时任务
			subCronExpr += res+",";
			if(StringUtils.isNotBlank(subCronExpr)) {
				subCronExpr = subCronExpr.substring(0, subCronExpr.lastIndexOf(","));
			}*/
//			0 0 */1 * * ?
            cronExpr = "0 0 */" + interval + " * * ?";
        }
        //封装更新任务定时器参数
        TaskInfoDO taskInfoDO = new TaskInfoDO();
        taskInfoDO.setCronExpr(cronExpr);
        taskInfoDO.setSbCode(sbCode);
        //根据sbCode更新定时器Cron表达式
        return taskInfoDao.updateBySbCodeSelective(taskInfoDO);
    }

    /**
     * @param sbCode
     * @return ScanRuleVO
     * @throws
     * @Title: getScanRuleInfo
     * @Description: 扫描规则详情
     */
    public ScanRuleVO getScanRuleInfo(String sbCode) {
        ScanRuleVO ruleVO = new ScanRuleVO();
        ScanRuleDO ruleDO = scanRuleDao.selectBySbCode(sbCode);
        if (ruleDO != null) {
            LsBeanUtils.copyAllProperties(ruleDO, ruleVO);
        }
        return ruleVO;
    }


    /**
     * @param sbCode
     * @return ScanRuleVO
     * @throws
     * @Title: getScanRuleInfo
     * @Description: 查询(1.出入库0.库存)
     */
    public ScanRuleVO getScanRuleIsOut(String sbCode) {
        ScanRuleVO ruleVO = new ScanRuleVO();
        ScanRuleDO ruleDO = scanRuleDao.selectByIsOut(sbCode);
        if (ruleDO != null) {
            LsBeanUtils.copyAllProperties(ruleDO, ruleVO);
        }
        return ruleVO;
    }

    public void deleteBySbCode(String sbCode){
        scanRuleDao.deleteBySbCode(sbCode);
    }
}
