package com.sw.mes.timer;

import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.utils.StringUtils;
import com.sw.mes.eam.repair.entity.RepairOrder;
import com.sw.mes.eam.repair.service.RepairOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @zhaoyayun
 * @20230427
 * @v1.0.0
 **/
@Component
public class RepairOederAutoConfirmTimer {

    @Autowired
    private RepairOrderService repairOrderService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private DictionaryItemService dictionaryItemService;


    //定时将待确认的维修工单更新为已确认状态
    //@Scheduled(cron = "0/5 * * * * ?")
    public void repairOderAutoConfirm() {

        try {
            Boolean lock = redisTemplate.opsForValue().setIfAbsent("lock", "repairOderAutoConfirm", 3, TimeUnit.SECONDS);
            if (lock){
                //业务代码
                List<RepairOrder> repairOrders = repairOrderService.findByStatus("WAIT_FOR_VERIFY");
                if (repairOrders.size()>0){

                    List<DictionaryItem> items = dictionaryItemService.findItemByThemeValueAndItemNameLikeAndEnable("repair_order_auto_confirm ", "自动确认时长", true);
                    if (items.size()>0){
                        String value = items.get(0).getValue();
                        if (!StringUtils.isEmpty(value) && !StringUtils.isEmpty(value) ){
                            for (RepairOrder repairOrder : repairOrders) {
                                if (null != repairOrder.getEndTime()){
                                    long endTime = repairOrder.getEndTime().getTime();
                                    long currentTimeMillis = System.currentTimeMillis();
                                    long confirmTime = Math.round(Double.valueOf(value)*3600000);

                                    if (currentTimeMillis - endTime >= confirmTime){
                                        repairOrder.setStatus("FINISHED");
                                        repairOrder.setConfirmTime(new Date());
                                        repairOrder.setConfirmUpdateTime(new Date());
                                        repairOrder.setConfirmUserName(BaseContextHandler.getUsername());
                                        repairOrderService.saveOrUpdate(repairOrder);
                                    }
                                }
                            }
                        }
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.redisTemplate.delete("lock");
        }
    }


}
