package com.cool.store.service.opening.impl;

import com.aliyun.ons20190214.models.OnsMessageTraceRequest;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.OpeningAcceptanceMapperDAO;
import com.cool.store.dao.OperationLogDAO;
import com.cool.store.dao.ShopInfoDAO;
import com.cool.store.dao.ShopStageInfoDAO;
import com.cool.store.entity.EnterpriseUserDO;
import com.cool.store.entity.OpeningAcceptanceDO;
import com.cool.store.entity.OperationLogDO;
import com.cool.store.entity.ShopAuditInfoDO;
import com.cool.store.enums.*;
import com.cool.store.enums.opening.AcceptanceStatusEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.OpeningAcceptanceMapper;
import com.cool.store.mapper.ShopAuditInfoMapper;
import com.cool.store.request.AuditApproveRequest;
import com.cool.store.request.opening.OpeningAcceptanceRequest;
import com.cool.store.service.OperationLogService;
import com.cool.store.service.PreparationService;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.service.opening.OpeningAcceptanceService;
import com.cool.store.utils.BeanUtil;
import com.cool.store.utils.poi.constant.Constants;
import com.cool.store.vo.opening.OpeningAcceptanceDetailVO;
import com.sun.xml.bind.v2.TODO;
import org.apache.commons.collections.ArrayStack;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.rowset.serial.SerialException;
import java.util.*;

import static com.cool.store.enums.UserRoleEnum.BRAND_HEAD;
import static com.cool.store.enums.UserRoleEnum.VICE_PRESIDENT_IN_CHARGE;

/**
 * @Auther zx_szh
 * @Date 2025/12/22 10:00
 * @Version 1.0
 */
@Service
public class OpeningAcceptanceServiceImpl implements OpeningAcceptanceService {

    @Resource
    private OpeningAcceptanceMapperDAO openingAcceptanceMapperDAO;
    @Resource
    OperationLogService operationLogService;
    @Resource
    ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    ShopAuditInfoMapper shopAuditInfoMapper;
    @Resource
    OperationLogDAO operationLogDAO;
    @Resource
    PreparationService  preparationService;
    @Resource
    UserAuthMappingService userAuthMappingService;
    @Resource
    ShopInfoDAO shopInfoDAO;



    @Override
    public Boolean addOpeningAcceptance(OpeningAcceptanceRequest request,LoginUserInfo user) {
        //新增开店验收数据
        if (request.getId()==null) {
            OpeningAcceptanceDO openingAcceptanceDO = new OpeningAcceptanceDO();
            openingAcceptanceDO.setStatus(AcceptanceStatusEnum.PENDING_ACCEPTANCE.getCode());
            openingAcceptanceDO.setCreateUser(user.getUserId());
            BeanUtil.copyProperties(request,openingAcceptanceDO);
            openingAcceptanceMapperDAO.addOpeningAcceptance(openingAcceptanceDO);
        }else {
            OpeningAcceptanceDO openingAcceptanceDO = new OpeningAcceptanceDO();
            openingAcceptanceDO.setStatus(AcceptanceStatusEnum.PENDING_ACCEPTANCE.getCode());
            openingAcceptanceDO.setUpdateUser(user.getUserId());
            openingAcceptanceDO.setUpdateTime(new Date());
            BeanUtil.copyProperties(request,openingAcceptanceDO);
            openingAcceptanceMapperDAO.updateOpeningAcceptance(openingAcceptanceDO);
        }
        //阶段状态变更
        shopStageInfoDAO.batchUpdateShopStageStatus(request.getShopId(), Collections.singletonList(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_272));

        //新增审核记录
        operationLogService.addOperationLog(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_270, user.getUserId(), user.getName(),
                OperationTypeEnum.OPERATION_TYPE_0, "开业验收提交", OperationStatusEnum.PROCESSED);

        //片区总顾问审核
        List<EnterpriseUserDO> users = userAuthMappingService.getUserIdByRoleEnumAndRegionId(Collections.singletonList(UserRoleEnum.OPERATION_GENERAL_CONSULTANT), shopInfoDAO.getShopInfo(request.getShopId()).getRegionId());
        operationLogService.addOperationLog(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_272,
                user.getUserId(), users,
                OperationTypeEnum.OPERATION_TYPE_1, "加盟签约合同审批", OperationStatusEnum.NOT_PROCESSED);

        return Boolean.TRUE;
    }

    @Override
    public OpeningAcceptanceDetailVO getAcceptanceDetail(Long shopId) {
        if (shopId==null){
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        OpeningAcceptanceDO opening = openingAcceptanceMapperDAO.getOpeningAcceptanceByShopId(shopId);
        OpeningAcceptanceDetailVO openingAcceptanceDetailVO = new OpeningAcceptanceDetailVO();
        BeanUtil.copyProperties(opening,openingAcceptanceDetailVO);
        return openingAcceptanceDetailVO;
    }

    @Override
    public Boolean auditOpeningAcceptance(AuditApproveRequest request, LoginUserInfo user) {
        OpeningAcceptanceDO open = openingAcceptanceMapperDAO.getOpeningAcceptanceByShopId(request.getShopId());
        if (open==null){
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        Integer status = open.getStatus();
        ShopSubStageStatusEnum nextStage = null;
        Integer resultType = null;
        if (Constants.ZERO_INTEGER.equals(request.getAuditResult())) {
            //驳回
            status = AcceptanceStatusEnum.ACCEPTANCE_IS_UNQUALIFIED.getCode();
            nextStage = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_274;
            resultType = Constants.ONE_INTEGER;
        }else {
            //通过
            status = AcceptanceStatusEnum.ACCEPTED.getCode();
            nextStage = ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_276;
            resultType = Constants.ZERO_INTEGER;
        }
        open.setStatus(status);
        open.setUpdateUser(user.getUserId());
        open.setUpdateTime(new Date());
        openingAcceptanceMapperDAO.updateOpeningAcceptance(open);
        //阶段
        shopStageInfoDAO.batchUpdateShopStageStatus(request.getShopId(), Arrays.asList(nextStage));
        ShopAuditInfoDO shopAuditInfoDO = new ShopAuditInfoDO();
        shopAuditInfoDO.setShopId(request.getShopId());
        shopAuditInfoDO.setAuditType(AuditTypeEnum.OPENING_ACCEPTANCE.getCode());
        shopAuditInfoDO.setSubmittedUserId(user.getUserId());
        shopAuditInfoDO.setSubmittedUserName(user.getName());
        shopAuditInfoDO.setResultType(resultType);
        shopAuditInfoDO.setRejectReason(request.getCause());
        shopAuditInfoMapper.insertSelective(shopAuditInfoDO);
        Long auditId = shopAuditInfoDO.getId();
        List<OperationLogDO> operationLogs = operationLogDAO.getBySubStageStatusEnumAndsStatus(request.getShopId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_272, OperationTypeEnum.OPERATION_TYPE_1.getCode());
        operationLogService.batchUpdateProcessed(operationLogs, auditId, user.getUserId(), request.getCause());
        //开业验收完成之后 开业日期报备打开
        if (status == AcceptanceStatusEnum.ACCEPTED.getCode()){
            preparationService.theOpeningDateIsReported(request.getShopId());
        }
        return Boolean.TRUE;
    }
}
