package cn.estsh.i3plus.ext.mes.apiservice.schedulejob;

import cn.estsh.i3plus.ext.mes.pojo.bean.MesNumberOfSafeOperationDays;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesNumberOfSafeOperationDaysRepository;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.impp.framework.base.schedule.BaseImppScheduleJob;
import cn.estsh.impp.framework.boot.init.ApplicationProperties;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

/**
 * 文件描述
 *
 * @author xiangming.liao
 * @date 2021年07月01日 16:00
 */
@Slf4j
@DisallowConcurrentExecution
@ApiOperation("安全运行天数，之后系统每天进行自增-job")
public class NumberOfSafeOperationDaysAutoSum extends BaseImppScheduleJob {
    @Autowired
    private MesNumberOfSafeOperationDaysRepository numberOfSafeOperationDaysRDao;

    public NumberOfSafeOperationDaysAutoSum() {
        super(NumberOfSafeOperationDaysAutoSum.class, "初始安全运行天数，之后系统每天进行自增-job");
    }

    @SneakyThrows
    @Override
    public void executeImppJob(JobExecutionContext context, ApplicationProperties applicationProperties) {
        String jobParam = this.getJobParam();
        if (StringUtils.isEmpty(jobParam)) {
            LOGGER.error("Mes业务数据同步定时任务,无法获取组织代码和方法代码");
            return;
        }

        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(jobParam);
        } catch (JSONException e) {
            LOGGER.error("Mes业务数据同步定时任务,参数异常，参数不是json格式");
            return;
        }
        Object organizeCode = jsonObject.get("organizationCode");
        log.info("安全运行天数-工厂代码：{}",organizeCode.toString());

        //查询初始安全天数信息
        DdlPackBean packBean = DdlPackBean.getDdlPackBean(organizeCode.toString());
        MesNumberOfSafeOperationDays mesNumberOfSafeOperationDays = numberOfSafeOperationDaysRDao.getByProperty(packBean);
        log.info("安全运行天数-mesNumberOfSafeOperationDays：{}",mesNumberOfSafeOperationDays);

        if (null != mesNumberOfSafeOperationDays) {
            //修改安全天数信息，进行自增

            String today = TimeTool.getToday();

            int initDays = TimeTool.getSecoundsBetweenTime(4, mesNumberOfSafeOperationDays.getInitEndDate(), today);
            mesNumberOfSafeOperationDays.setInitEndDate(today);
            log.info("安全运行天数-initDays：{}",initDays);
            mesNumberOfSafeOperationDays.setInitDays(mesNumberOfSafeOperationDays.getInitDays() + initDays);
            log.info("安全运行天数-numberOfSafeOperationDaysRDao.save：{}",mesNumberOfSafeOperationDays);
            ConvertBean.serviceModelUpdate(mesNumberOfSafeOperationDays,"JOB");
            numberOfSafeOperationDaysRDao.save(mesNumberOfSafeOperationDays);
        }


    }
}
