package com.sw.common.accessory.server.service.impl;


import com.sw.common.accessory.server.EnumValuesAccy;
import com.sw.common.accessory.server.entity.Accessory;
import com.sw.common.accessory.server.entity.LifeResume;
import com.sw.common.accessory.server.mapper.LifeResumeMapper;
import com.sw.common.accessory.server.service.LifeResumeService;
import com.sw.common.core.context.BaseContextHandler;
import com.sw.common.core.enums.EnumDictUtil;
import com.sw.common.core.exception.BusinessException;
import com.sw.common.mybatisplus.base.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import static com.sw.common.accessory.config.Constant.ENUM_DICT_ACCY_LIFE_TYPE;

;


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class LifeResumeServiceImpl extends BaseServiceImpl<LifeResumeMapper, LifeResume> implements LifeResumeService {
    /**
     * @param accOper   操作类型
     * @param accessory 辅材信息
     */
    @Override
    public void addLifeLog(EnumValuesAccy.AccOper accOper, Accessory accessory, Accessory targetAccessory) {
        List<LifeResume> lifeResumes = new LinkedList<>();
        LifeResume lifeResume = new LifeResume();
        lifeResume.setAccessoryId(accessory.getId());
        lifeResume.setUserCreate(BaseContextHandler.getUsername());
        lifeResume.setOperateTime(new Date());
        lifeResume.setType(accOper.getKey());
        lifeResume.setEquipmentCode(accessory.getEquipmentCode());
        String lifeTypeName = EnumDictUtil.getLabel(ENUM_DICT_ACCY_LIFE_TYPE, accessory.getLifeType()).toString();
        //上机时间
        if(!ObjectUtils.isEmpty(accessory.getUpperTime())){
            Date upperTime = accessory.getUpperTime();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(upperTime);
            int upDay = calendar.get(Calendar.DAY_OF_MONTH);
            int upHour = calendar.get(Calendar.HOUR_OF_DAY);
            //下机时间
            Calendar instance = Calendar.getInstance();
            //下机状态
            if(!ObjectUtils.isEmpty(accessory.getDownTime()) && accessory.getStatus().equals(0)){
                instance.setTime(accessory.getDownTime());
            }
            int downDay = instance.get(Calendar.DAY_OF_MONTH);
            int downHour = instance.get(Calendar.HOUR_OF_DAY);
            //上机12点之前&&下机12点之后,上机12点之后||下机12点之前,上机12点之后&&下机12点之前
            int day;
            if (upHour < 12 && downHour >= 12) {
                day = downDay - upDay + 1;
            } else if ((upHour >= 12 && downHour >= 12) || (upHour < 12 && downHour < 12)) {
                day = downDay - upDay;
            } else {
                day = downDay - upDay - 1;
            }
            accessory.setUsefulLife(day);
            Integer ratedLife = accessory.getRatedLife();
            accessory.setRemainingLife(ratedLife - day);
        }
        switch (accOper) {
            case UpperDevice:
                lifeResume.setMessage(
                        " <div style=\"display: flex;align-items: center;margin-bottom: 10px;\">\n" +
                                "   <div style=\"padding: 3px 15px;background-color: rgb(112, 211, 102);border-radius: 20px;color: #FFF;margin-right: 5px;min-width: 60px;\">上机</div>\n" +
                                "   <div>设备号<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getEquipmentCode() + "</span>物料上机,物料已使用寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getUsefulLife() + "/（" + lifeTypeName + "）</span>,剩余寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getRemainingLife() + "（" + lifeTypeName + "）</span>,操作原因:<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">上机</span> ，操作人:<span style=\"color: black;margin: 0px 5px;font-weight: bold;\">" + accessory.getUserCreate() + "</span></div>\n" +
                                " </div>");
                break;
            case ApplyForStockReturn:
                lifeResume.setMessage(
                        "<div style=\"display: flex;align-items: center;margin-bottom: 10px;\">\n" +
                                "   <div style=\"padding: 3px 15px;background-color: rgb(240, 210, 45);border-radius: 20px;color: #FFF;margin-right: 5px;min-width: 60px;\">下机</div>\n" +
                                "   <div>设备号<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getEquipmentCode() + "</span>物料下机,物料已使用寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getUsefulLife() + "（" + lifeTypeName + "）</span>,剩余寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getRemainingLife() + "（" + lifeTypeName + "）</span>,操作原因:<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">申请退库（下机）</span>，操作人:<span style=\"color: black;margin: 0px 5px;font-weight: bold;\">" + accessory.getUserCreate() + "</span></div>\n" +
                                " </div>");
                break;
            case Transfer:
                Assert.notNull(targetAccessory, "对调后的辅材信息不能为空");
                lifeResume.setMessage(
                        " <div style=\"display: flex;align-items: center;margin-bottom: 10px;\">\n" +
                                "   <div style=\"padding: 3px 15px;background-color: rgb(240, 210, 45);border-radius: 20px;color: #FFF;margin-right: 5px;min-width: 60px;\">下机</div>\n" +
                                "   <div>设备号<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + targetAccessory.getEquipmentCode() + "</span>物料对调下机,物料已使用寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getUsefulLife() + "（" + lifeTypeName + "）</span>,剩余寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getRemainingLife() + "（" + lifeTypeName + "）</span>,操作原因:<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">对调</span>，操作人:<span style=\"color: black;margin: 0px 5px;font-weight: bold;\">" + accessory.getUserCreate() + "</span></div>\n" +
                                " </div>" +
                                "<div style=\"display: flex;align-items: center;margin-bottom: 10px;\">\n" +
                                "  <div style=\"padding: 3px 15px;background-color: rgb(112, 211, 102);border-radius: 20px;color: #FFF;margin-right: 5px;min-width: 60px;\">上机</div>\n" +
                                "  <div>设备号<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getEquipmentCode() + "</span>物料对调上机，操作人:<span style=\"color: black;margin: 0px 5px;font-weight: bold;\">" + accessory.getUserCreate() + "</span></div>\n" +
                                "</div>"
                );
                LifeResume targetLifeResume = new LifeResume();
                targetLifeResume.setAccessoryId(targetAccessory.getId());
                targetLifeResume.setUserCreate(BaseContextHandler.getUsername());
                targetLifeResume.setOperateTime(new Date());
                targetLifeResume.setType(accOper.getKey());
                targetLifeResume.setEquipmentCode(targetAccessory.getEquipmentCode());
                String targetLifeTypeName = EnumDictUtil.getLabel(ENUM_DICT_ACCY_LIFE_TYPE, targetAccessory.getLifeType()).toString();
                targetLifeResume.setMessage(
                        " <div style=\"display: flex;align-items: center;margin-bottom: 10px;\">\n" +
                                "   <div style=\"padding: 3px 15px;background-color: rgb(240, 210, 45);border-radius: 20px;color: #FFF;margin-right: 5px;min-width: 60px;\">下机</div>\n" +
                                "   <div>设备号<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getEquipmentCode() + "</span>物料机对调下机,物料已使用寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + targetAccessory.getUsefulLife() + "（" + targetLifeTypeName + "）</span>,剩余寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + targetAccessory.getRemainingLife() + "（" + targetLifeTypeName + "）</span>,操作原因:<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">对调</span>，操作人:<span style=\"color: black;margin: 0px 5px;font-weight: bold;\">" + targetAccessory.getUserCreate() + "</span></div>\n" +
                                " </div>" +
                                "<div style=\"display: flex;align-items: center;margin-bottom: 10px;\">\n" +
                                "  <div style=\"padding: 3px 15px;background-color: rgb(112, 211, 102);border-radius: 20px;color: #FFF;margin-right: 5px;min-width: 60px;\">上机</div>\n" +
                                "  <div>设备号<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + targetAccessory.getEquipmentCode() + "</span>物料对调上机，操作人:<span style=\"color: black;margin: 0px 5px;font-weight: bold;\">" + targetAccessory.getUserCreate() + "</span></div>\n" +
                                "</div>"
                );
                lifeResumes.add(targetLifeResume);
                break;
            case LifeConsumption:
                lifeResume.setMessage(
                        "<div style=\"display: flex;align-items: center;margin-bottom: 10px;\">\n" +
                                "   <div style=\"padding: 3px 15px;background-color: #f56c6c;border-radius: 20px;color: #FFF;margin-right: 5px;min-width: 60px;\">寿命消耗</div>\n" +
                                "   <div>设备号<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getEquipmentCode() + "</span>物料机消耗, 物料已使用寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getUsefulLife() + "（" + lifeTypeName + "）</span>,剩余寿命<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">" + accessory.getRemainingLife() + "（" + lifeTypeName + "）</span>,操作原因:<span style=\"color: rgb(39, 167, 206);margin: 0px 5px;\">寿命消耗</span>，操作人:<span style=\"color: black;margin: 0px 5px;font-weight: bold;\">" + accessory.getUserCreate() + "</span></div>\n" +
                                " </div>");
                break;
            default:
                throw new BusinessException("操作类型不存在");
        }
        lifeResumes.add(lifeResume);
        saveAll(lifeResumes);
    }
}