package com.cool.store.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.cool.store.context.CurrentUserHolder;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.*;
import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.entity.OpeningOperationPlanDO;
import com.cool.store.entity.ShopAuditInfoDO;
import com.cool.store.enums.AuditResultTypeEnum;
import com.cool.store.enums.AuditTypeEnum;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.OpeningOperationPlanResultTypeEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.OpeningOperationPlanAuditRequest;
import com.cool.store.service.AuditOpeningOperationPlanService;
import com.cool.store.service.PreparationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Auther: WangShuo
 * @Date: 2024/04/22/下午3:19
 * @Version 1.0
 * @注释:
 */
@Slf4j
@Service
public class AuditOpeningOperationPlanImpl implements AuditOpeningOperationPlanService {


    @Resource
    private OpeningOperationPlanDAO openingOperationPlanDAO;
    @Resource
    private ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    private PreparationService preparationService;
    @Resource
    private ShopAuditInfoDAO shopAuditInfoDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean auditPlan(OpeningOperationPlanAuditRequest request, LoginUserInfo user) {
        log.info("auditPlan request:{}", JSONObject.toJSONString(request));
        if (Objects.isNull(request)) {
            log.error("auditPlan request  is null");
            throw new ServiceException("auditPlan request is null");
        }
        try {
            ShopAuditInfoDO shopAuditInfoDO = request.toShopAuditInfoDO();
            shopAuditInfoDO.setSubmittedUserId(user.getUserId());
            shopAuditInfoDO.setSubmittedUserName(user.getName());
            Long auditId = shopAuditInfoDAO.addAuditInfo(shopAuditInfoDO);
            OpeningOperationPlanDO openingOperationPlanDO = new OpeningOperationPlanDO();
            openingOperationPlanDO.setShopId(request.getShopId());
            openingOperationPlanDO.setAuditId(auditId);
            openingOperationPlanDO.setResultType(request.getResultType());
            openingOperationPlanDAO.updateResultType(request.getShopId(),auditId,request.getResultType());
            ShopSubStageStatusEnum shopSubStageStatusEnum = null;
            Boolean flag = Boolean.FALSE;
            if(OpeningOperationPlanResultTypeEnum.PASS_AUDIT.getCode().equals(request.getResultType())){
                shopSubStageStatusEnum = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_143;
                flag = Boolean.TRUE;
            }else {
                shopSubStageStatusEnum = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_142;
            }
            shopStageInfoDAO.updateShopStageAndAuditInfo(request.getShopId(),
                    shopSubStageStatusEnum, auditId);
            if (flag){
                preparationService.whetherToOpenForAcceptance(request.getShopId());
            }
            return Boolean.TRUE;
        } catch (Exception e) {
            log.error("auditPlan Exception:{}", e);
            return Boolean.FALSE;
        }

    }


}
