package com.sw.mes.fb.checkout.service.impl;


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.entity.FbCheckOutData;
import com.sw.mes.fb.checkout.dao.PickUpCheckDao;
import com.sw.mes.fb.checkout.entity.PickUpCheck;
import com.sw.mes.fb.checkout.service.FbCheckOutDataService;
import com.sw.mes.fb.checkout.service.PickUpCheckService;
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.Optional;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class PickUpCheckServiceImpl extends BaseServiceImpl<PickUpCheck> implements PickUpCheckService {

    @Autowired
    private FbCheckOutDataService fbCheckOutDataService;

    PickUpCheckDao dao;
	@Autowired
    public void setDao(PickUpCheckDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Override
    public void createPickUpCheck(PickUpCheck pickUpCheck) {
        FbCheckOutData data = fbCheckOutDataService.findDataByFbSectionNo(pickUpCheck.getSectionNo());
        if (data != null && pickUpCheck.getEnable() != null){
            data.setEnableCheck(pickUpCheck.getEnable());
            fbCheckOutDataService.saveOrUpdate(data);
        }
        pickUpCheck.setEquipmentCode(Optional.ofNullable(data).map(FbCheckOutData::getDeviceNo).orElse(""));
        pickUpCheck.setSpecification(Optional.ofNullable(data).map(FbCheckOutData::getSpecification).orElse(""));
        pickUpCheck.setCheckType("抽检");
        pickUpCheck.setCreateUser(BaseContextHandler.getUsername());
        pickUpCheck.setCreateTime(new Date());
        saveOrUpdate(pickUpCheck);
    }

    @Override
    public void updatePickUpCheck(PickUpCheck pickUpCheck) {
        FbCheckOutData data = fbCheckOutDataService.findDataByFbSectionNo(pickUpCheck.getSectionNo());
        if (data != null && pickUpCheck.getEnable() != null){
            data.setEnableCheck(pickUpCheck.getEnable());
            fbCheckOutDataService.saveOrUpdate(data);
        }
        pickUpCheck.setUpdateUser(BaseContextHandler.getUsername());
        pickUpCheck.setUpdateTime(new Date());
        saveOrUpdate(pickUpCheck);
    }

}
