package com.sw.mes.yb.accessory.service.impl;


import cn.hutool.Hutool;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.enums.EnumValues;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.yb.accessory.dao.AccessoryDiscardDao;
import com.sw.mes.yb.accessory.dto.AccessoryDiscardExamineDto;
import com.sw.mes.yb.accessory.entity.AccessoryDiscard;
import com.sw.mes.yb.accessory.entity.AccessoryReturn;
import com.sw.mes.yb.accessory.service.AccessoryDiscardService;
import com.sw.mes.yb.accessory.service.AccessoryReturnService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class AccessoryDiscardServiceImpl extends BaseServiceImpl<AccessoryDiscard> implements AccessoryDiscardService {
    AccessoryDiscardDao dao;
	@Autowired
    public void setDao(AccessoryDiscardDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    private AccessoryReturnService accessoryReturnService;

    @Override
    public void examine(AccessoryDiscardExamineDto examineDto) {
        AccessoryDiscard accessoryDiscard = get(examineDto.getId());
        BeanUtils.copyProperties(examineDto, accessoryDiscard, BeanUtil.getIgnoreProperties(examineDto, null, null));
        if (compare2Str(examineDto.getOpinionOfProduct(), accessoryDiscard.getOpinionOfProduct())){
            accessoryDiscard.setTimeOfProduct(new Date());
        }
        if (compare2Str(examineDto.getOpinionOfTechnology(), accessoryDiscard.getOpinionOfTechnology())){
            accessoryDiscard.setTimeOfTechnology(new Date());
        }
        if (compare2Str(examineDto.getOpinionOfQuality(), accessoryDiscard.getOpinionOfQuality())){
            accessoryDiscard.setTimeOfQuality(new Date());
        }
        accessoryDiscard.setUpdateId(BaseContextHandler.getUserID());
        accessoryDiscard.setUpdateUser(BaseContextHandler.getUsername());
        accessoryDiscard.setUpdateTime(new Date());

        if (StringUtils.isNotBlank(accessoryDiscard.getOpinionOfProduct())
                || StringUtils.isNotBlank(accessoryDiscard.getOpinionOfTechnology())
                || StringUtils.isNotBlank(accessoryDiscard.getOpinionOfQuality())){
            accessoryDiscard.setResult(EnumValues.AccessoryOpinionStatus.OPINIONING.getKey()); //有一个进行评审就是评审中
        }

        if (StringUtils.isNotBlank(accessoryDiscard.getOpinionOfProduct())
                && StringUtils.isNotBlank(accessoryDiscard.getOpinionOfTechnology())
                && StringUtils.isNotBlank(accessoryDiscard.getOpinionOfQuality())){
            accessoryDiscard.setResult(EnumValues.AccessoryOpinionStatus.FINISHOPINION.getKey()); //都评审了就是评审完毕


            AccessoryReturn accessoryReturn = accessoryDiscard.getAccessoryReturn();
            if ("是".equals(accessoryDiscard.getOpinionOfProduct())
                    && "是".equals(accessoryDiscard.getOpinionOfTechnology())
                    && "是".equals(accessoryDiscard.getOpinionOfQuality())){
                accessoryReturn.setState(EnumValues.AccessoryReturnStatus.CONFIRMED.getKey());
                //TODO 将审核情况给线边仓
            }else {
                accessoryReturn.setState(EnumValues.AccessoryReturnStatus.REJECTED.getKey());
                //TODO 将审核情况给线边仓
            }
        }

    }

    @Override
    public AccessoryDiscard findByReturnId(Integer id) {
        List<AccessoryDiscard> list = dao.findByReturnId(id);
        if (list.size() > 0){
            return list.get(0);
        }
        return null;
    }

    public boolean compare2Str(String opinionDto, String opinion){
        return StringUtils.isNotBlank(opinionDto)
                && (StringUtils.isBlank(opinion) || !opinionDto.equals(opinion));
    }
}
