# Edc2MyspcServiceImpl.java

    package com.mycim.server.edcmyspc.service;

    import com.alipay.sofa.runtime.api.annotation.SofaService;
    import com.alipay.sofa.runtime.api.annotation.SofaServiceBinding;
    import com.fa.sesa.exception.Assert;
    import com.fa.sesa.exception.Errors;
    import com.fa.sesa.threadlocal.LocalContext;
    import com.fa.sesa.threadlocal.LocalContextNames;
    import com.mycim.server.edcchart.manager.EquipmentChartManager;
    import com.mycim.server.edcmyspc.manager.Edc2MyspcManager;
    import com.mycim.server.security.manager.UserManager;
    import com.mycim.server.system.manager.FacilityManager;
    import com.mycim.valueobject.edcspc.dto.SpcHoldEquipmentRequestDto;
    import com.mycim.valueobject.edcspc.dto.UploadChartInfoDto;
    import com.mycim.valueobject.security.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    /**
     * @author Luopeng.Wang
     * @version 6.0.0
     * @date 2019/11/22
     **/
    @Service
    @SofaService(interfaceType = Edc2MyspcService.class, bindings = {@SofaServiceBinding(bindingType = "bolt")})
    public class Edc2MyspcServiceImpl implements Edc2MyspcService {
        @Autowired
        private Edc2MyspcManager edc2MyspcManager;

        @Autowired
        private FacilityManager facilityManager;

        @Autowired
        private UserManager userManager;

        @Autowired
        private EquipmentChartManager equipmentChartManager;

        @Override
        public void holdEquipmentForMyspc(SpcHoldEquipmentRequestDto spcHoldEquipmentRequestDto) {
            initLocalContextForMyspc();
            edc2MyspcManager.holdEquipmentForMyspc(spcHoldEquipmentRequestDto);
        }

        @Override
        public void uploadChartInfo(UploadChartInfoDto uploadChartInfoDto) {
            initLocalContextForMyspc();
            equipmentChartManager.uploadChartInfos(uploadChartInfoDto);
        }

        @Override
        public void deleteChartInfo(UploadChartInfoDto uploadChartInfoDto) {
            initLocalContextForMyspc();
            equipmentChartManager.deleteChartInfo(uploadChartInfoDto);
        }

        private void initLocalContextForMyspc() {
            long facilityRrn = LocalContext.getFacilityRrn();
            if (facilityRrn <= 0) {
                String facilityId = (String) LocalContext.get(LocalContextNames.FACILITY_ID_KEY);
                facilityRrn = facilityManager.getFacilityRrn(facilityId);

                Assert.state(facilityRrn > 0, Errors.create().content("Facility does not exist.").build());

                LocalContext.put(LocalContextNames.FACILITY_RRN_KEY, String.valueOf(facilityRrn));

            }
            long userRrn = LocalContext.getUserRrn();
            if (userRrn <= 0) {
                User user = userManager.getUser(LocalContext.getUserId());
                Assert.isFalse(user == null || user.getInstanceRrn() <= 0,
                               Errors.create().content("User does not exist.").build());
                userRrn = user.getInstanceRrn();
                LocalContext.put(LocalContextNames.USER_RRN_KEY, String.valueOf(userRrn));
            }

        }

    }