package com.cool.store.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.dao.*;
import com.cool.store.entity.*;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.SMSMsgEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.*;
import com.cool.store.service.DecorationDesignInfoService;
import com.cool.store.utils.poi.StringUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author suzhuhong
 * @Date 2024/6/20 17:22
 * @Version 1.0
 */
@Service
public class DecorationDesignInfoServiceImpl implements DecorationDesignInfoService {

    @Resource
    private AcceptanceInfoDAO acceptanceInfoDAO;
    @Resource
    private CommonService commonService;
    @Resource
    private DecorationDesignInfoDAO decorationDesignInfoDAO;
    @Resource
    private ShopInfoDAO shopInfoDAO;
    @Resource
    private ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    private LineInfoDAO lineInfoDAO;
    @Resource
    private BuildInformationDAO buildInformationDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean assignDesigner(AssignDesignerRequest request) {

        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_9);
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_00.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        DecorationDesignInfoDO designInfoDO = decorationDesignInfoDAO.getByShopId(shopInfoDO.getId());
        if (designInfoDO == null) {
            designInfoDO = new DecorationDesignInfoDO();
            designInfoDO.setShopId(shopInfoDO.getId());
            designInfoDO.setCreateTime(new Date());
        }
        designInfoDO.setDesignName(request.getDesignName());
        designInfoDO.setDesignSubmitTime(new Date());
        designInfoDO.setDesignSheetId(request.getDesignSheetId());
        designInfoDO.setUpdateTime(new Date());
        if (designInfoDO.getId() == null) {
            decorationDesignInfoDAO.insertSelective(designInfoDO);
        } else {
            decorationDesignInfoDAO.updateByPrimaryKeySelective(designInfoDO);
        }

        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_90.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_901);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitDesignDrawing(SubmitDesignDrawingRequest request) {

        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        DecorationDesignInfoDO designInfoDO = decorationDesignInfoDAO.getByShopId(shopInfoDO.getId());
        if (designInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        designInfoDO.setDesignSheetId(request.getDesignSheetId());
        designInfoDO.setDesignName(request.getDesignName());
        designInfoDO.setOriginalPhotoUrl(String.join(",", request.getOriginalPhotoUrl()));
        designInfoDO.setRenderingUrl(String.join(",", request.getRenderingUrl()));
        designInfoDO.setFloorPlanUrl(String.join(",", request.getFloorPlanUrl()));
        designInfoDO.setUpdateTime(new Date());
        designInfoDO.setDesignSubmitTime(new Date());
        decorationDesignInfoDAO.updateByPrimaryKeySelective(designInfoDO);
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_9);
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_901.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_902);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean assignQuotation(AssignQuotationRequest request) {

        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        DecorationDesignInfoDO designInfoDO = decorationDesignInfoDAO.getByShopId(shopInfoDO.getId());
        if (designInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        designInfoDO.setQuotationSheetId(request.getQuotationSheetId());
        designInfoDO.setQuoter(request.getQuoter());
        designInfoDO.setQuotationSubmitTime(new Date());
        designInfoDO.setUpdateTime(new Date());
        decorationDesignInfoDAO.updateByPrimaryKeySelective(designInfoDO);
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_9);
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_902.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_903);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitQuotationSheet(SubmitQuotationSheetRequest request) {

        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        LineInfoDO lineInfoDO = lineInfoDAO.getLineInfo(shopInfoDO.getLineId());
        DecorationDesignInfoDO designInfoDO = decorationDesignInfoDAO.getByShopId(shopInfoDO.getId());
        if (designInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        designInfoDO.setQuotationSheetId(request.getQuotationSheetId());
        designInfoDO.setQuotationUrl(String.join(",", request.getQuotationSheetUrl()));
        designInfoDO.setQuotationSubmitTime(new Date());
        designInfoDO.setUpdateTime(new Date());
        decorationDesignInfoDAO.updateByPrimaryKeySelective(designInfoDO);
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_9);
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_903.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_905);
            Map<String, String> templateParam = new HashMap<>();
            templateParam.put("shopName", shopInfoDO.getShopName());
            commonService.sendSms(lineInfoDO.getMobile(), SMSMsgEnum.DESIGN_STAGE, templateParam);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean submitConstructionOrder(ConstructionOrderRequest request) {

        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        DecorationDesignInfoDO designInfoDO = decorationDesignInfoDAO.getByShopId(shopInfoDO.getId());
        if (designInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        designInfoDO.setConstructionOrderId(request.getConstructionOrderId());
        designInfoDO.setConstructionPlanStartTime(request.getConstructionPlanStartTime());
        designInfoDO.setConstructionPlanEndTime(request.getConstructionPlanEndTime());
        decorationDesignInfoDAO.updateByPrimaryKeySelective(designInfoDO);
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_11);
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_110.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_110_5);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean entryConfirmation(EntryConfirmationRequest request) {

        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        DecorationDesignInfoDO designInfoDO = decorationDesignInfoDAO.getByShopId(shopInfoDO.getId());
        if (designInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        designInfoDO.setConstructionOrderId(request.getConstructionOrderId());
        designInfoDO.setActualEntryTime(request.getActualEntryTime());
        decorationDesignInfoDAO.updateByPrimaryKeySelective(designInfoDO);
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_11);
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_110_5.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_111);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean constructionCompleted(ConstructionCompletedRequest request) {

        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        DecorationDesignInfoDO designInfoDO = decorationDesignInfoDAO.getByShopId(shopInfoDO.getId());
        if (designInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        designInfoDO.setConstructionOrderId(request.getConstructionOrderId());
        decorationDesignInfoDAO.updateByPrimaryKeySelective(designInfoDO);
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_11);
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_111.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_112);
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_121);
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean decorationAcceptance(HqtDecorationAcceptanceRequest request) {
        ShopInfoDO shopInfoDO = shopInfoDAO.getShopInfo(request.getShopId());
        if (shopInfoDO == null) {
            throw new ServiceException(ErrorCodeEnum.SHOP_NOT_EXIST);
        }
        if (StringUtils.isEmpty(shopInfoDO.getHqtShopId())){
            throw new ServiceException(ErrorCodeEnum.HQT_SHOP_ID_NOT_EXIST);
        }
        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfoDO.getId(), ShopSubStageEnum.SHOP_STAGE_12);
        if (!shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_122.getShopSubStageStatus())
                && !shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_123.getShopSubStageStatus())) {
            throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_NOT_OPERATE);
        }
        AcceptanceInfoDO acceptanceInfoDO = acceptanceInfoDAO.selectByShopId(shopInfoDO.getId());
        if (Objects.isNull(acceptanceInfoDO)) {
            acceptanceInfoDO = new AcceptanceInfoDO();
            acceptanceInfoDO.setShopId(shopInfoDO.getId());
        }
        acceptanceInfoDO.setDeliveryOrderId(request.getDeliveryOrderId());
        acceptanceInfoDO.setActualAcceptanceTime(request.getActualAcceptanceTime());
        acceptanceInfoDO.setAcceptanceAttachment(String.join(",", request.getAcceptanceAttachmentUrl()));
        acceptanceInfoDO.setShopDoorwayPhoto(String.join(",",request.getShopDoorwayPhotoUrl()));
        acceptanceInfoDO.setShopInteriorPhoto(String.join(",",request.getShopInteriorPhotoUrl()));
        acceptanceInfoDO.setBackKitchenPhoto(String.join(",",request.getBackKitchenPhotoUrl()));
        acceptanceInfoDO.setAddReduceAmount(request.getAddReduceAmount());
        if (acceptanceInfoDO.getId() == null) {
            acceptanceInfoDAO.insertSelectiveAcceptanceInfo(acceptanceInfoDO);
        } else {
            acceptanceInfoDAO.updateAcceptanceInfo(acceptanceInfoDO);
        }
        if (shopSubStageInfo.getShopSubStageStatus().equals(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_122.getShopSubStageStatus())) {
            shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_123);
        }
        // 覆盖建店资料中的门头照和内景照
        BuildInformationDO buildInformation = BuildInformationDO.builder().shopId(request.getShopId()).doorPhoto(buildJson(request.getShopDoorwayPhotoUrl()))
                .inStorePhoto(buildJson(request.getShopInteriorPhotoUrl())).build();
        buildInformationDAO.updateByShopIdSelective(buildInformation);
        return true;
    }

    public String buildJson(List<String> urls) {
        if (CollectionUtils.isEmpty(urls)) {
            return null;
        }
        JSONArray jsonArray = new JSONArray();
        for (String url : urls) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("url", url);
            jsonObject.put("type", extractImageSuffix(url));
            jsonArray.add(jsonObject);
        }
        return jsonArray.toJSONString();
    }

    public String extractImageSuffix(String url) {
        return url.substring(url.lastIndexOf(".") + 1);
    }
}
