package com.cool.store.service.impl;

import com.cool.store.dao.*;
import com.cool.store.entity.*;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.MessageEnum;
import com.cool.store.enums.SMSMsgEnum;
import com.cool.store.enums.UserRoleEnum;
import com.cool.store.enums.point.*;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.AddShopRequest;
import com.cool.store.request.DeleteShopRequest;
import com.cool.store.request.UpdateShopCodeRequest;
import com.cool.store.service.ShopService;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.utils.NumberConverter;
import com.cool.store.utils.RandomEightCharCodeUtils;
import com.cool.store.vo.shop.MiniShopPageVO;
import com.cool.store.vo.shop.ShopStageInfoVO;
import com.cool.store.vo.shop.StageShopCountVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @author zhangchenbiao
 * @FileName: ShopServiceImpl
 * @Description:
 * @date 2024-04-11 10:26
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Resource
    private ShopInfoDAO shopInfoDAO;
    @Resource
    private ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    private ShopAuditInfoDAO shopAuditInfoDAO;
    @Resource
    private LineInfoDAO lineInfoDAO;
    @Resource
    private PointInfoDAO pointInfoDAO;
    @Resource
    private PointRecommendDAO pointRecommendDAO;
    @Resource
    private CommonService commonService;
    @Resource
    UserAuthMappingService userAuthMappingService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer initShop(LineInfoDO lineInfo) {
        if(Objects.isNull(lineInfo)){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        Integer wantShopNum = lineInfo.getWantShopNum();
        List<ShopInfoDO> shopList = shopInfoDAO.getShopList(lineInfo.getId());
        int shopSize = shopList.size();
        if(shopSize >= wantShopNum){
            return 0;
        }
        List<ShopInfoDO> addShopList = new ArrayList<>();
        for (int i = shopSize; i < wantShopNum; i++) {
            ShopInfoDO shopInfo = new ShopInfoDO();
            shopInfo.setRegionId(lineInfo.getRegionId());
            shopInfo.setLineId(lineInfo.getId());
            shopInfo.setPartnerId(lineInfo.getPartnerId());
            //初始化
            shopInfo.setStoreNum(RandomEightCharCodeUtils.getCode());
            shopInfo.setSupervisorUserId(lineInfo.getInvestmentManager());
            shopInfo.setShopName("店铺" + NumberConverter.convertArabicToChinese(i + 1));
            addShopList.add(shopInfo);
        }
        shopInfoDAO.batchAddShop(addShopList);
        List<Long> shopIds = addShopList.stream().map(ShopInfoDO::getId).collect(Collectors.toList());
        Integer result = shopStageInfoDAO.initShopStageInfo(lineInfo.getId(), shopIds);
        Map<String, String> requestMap = new HashMap<>();
//        for (Long shopId : shopIds) {
//            requestMap.put("partnerName",lineInfo.getUsername());
//            requestMap.put("partnerMobile",lineInfo.getMobile());
//            requestMap.put("lineId",String.valueOf(lineInfo.getId()));
//            requestMap.put("shopId",String.valueOf(shopId));
//            commonService.sendMessage(Arrays.asList(lineInfo.getInvestmentManager()), MessageEnum.MESSAGE_23,requestMap);
//        }
        return result;
    }

    @Override
    public List<MiniShopPageVO> getShopList(Long lineId) {
        List<ShopInfoDO> shopList = shopInfoDAO.getShopList(lineId);
        List<Long> shopIds = shopList.stream().map(ShopInfoDO::getId).collect(Collectors.toList());
        List<ShopStageInfoDO> subStageList = shopStageInfoDAO.getSubStageList(shopIds, ShopSubStageEnum.SHOP_STAGE_8.getShopSubStage());
        Map<Long, ShopStageInfoDO> stageMap = subStageList.stream().collect(Collectors.toMap(ShopStageInfoDO::getShopId, Function.identity()));
        return MiniShopPageVO.convertList(shopList,stageMap);
    }

    @Override
    public List<ShopStageInfoVO> getShopStageInfo(Long shopId, Integer shopStage) {
        if(Objects.nonNull(shopStage)){
            ShopStageEnum shopStageEnum = ShopStageEnum.getShopStageEnum(shopStage);
            if(Objects.isNull(shopStageEnum)){
                throw new ServiceException(ErrorCodeEnum.SHOP_STAGE_ERROR);
            }
        }
        List<ShopStageInfoDO> shopStageInfo = shopStageInfoDAO.getShopStageInfo(shopId, shopStage);
        List<Long> auditIds = shopStageInfo.stream().filter(o->Objects.nonNull(o.getAuditId())).map(ShopStageInfoDO::getAuditId).distinct().collect(Collectors.toList());
        List<ShopAuditInfoDO> auditList = shopAuditInfoDAO.getAuditInfoList(auditIds);
        return ShopStageInfoVO.convertList(shopStageInfo, auditList);
    }

    @Override
    public StageShopCountVO getStageShopCount(Long lineId) {
        return shopInfoDAO.getStageShopCount(lineId);
    }

    @Override
    public Integer deleteShop(DeleteShopRequest request) {
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(request.getShopId());
        if(Objects.nonNull(shopInfo)){
            //进入选址不允许删除操作
            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
        }
//        ShopStageInfoDO shopSubStageInfo = shopStageInfoDAO.getShopSubStageInfo(shopInfo.getId(), ShopSubStageEnum.SHOP_STAGE_3);
//        if(ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_33.getShopSubStageStatus().equals(shopSubStageInfo.getShopSubStageStatus())){
//            throw new ServiceException(ErrorCodeEnum.NOT_ALLOW_OPERATE);
//        }
        if(Objects.nonNull(shopInfo.getPointId())){
            PointInfoDO pointInfo = pointInfoDAO.getPointInfoById(shopInfo.getPointId());
            if(Objects.nonNull(pointInfo) && SelectStatusEnum.SELECT_STATUS_1.getCode().equals(pointInfo.getSelectStatus())){
                pointInfoDAO.recyclePoint(pointInfo.getId());
            }
            pointRecommendDAO.updateRecommendStatus(shopInfo.getPointId(), PointRecommendStatusEnum.POINT_RECOMMEND_STATUS_1, Arrays.asList(PointRecommendStatusEnum.POINT_RECOMMEND_STATUS_5));
        }
        ShopInfoDO updateShop = new ShopInfoDO();
        updateShop.setId(request.getShopId());
        updateShop.setDeleted(true);
        return shopInfoDAO.updateShopInfo(updateShop);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long addShop(AddShopRequest request) {
        LineInfoDO lineInfo = lineInfoDAO.getLineInfo(request.getLineId());
        if(Objects.isNull(lineInfo)){
            throw new ServiceException(ErrorCodeEnum.LINE_ID_IS_NOT_EXIST);
        }
        List<String> shopNameList = request.getShopNameList();
        List<ShopInfoDO> shopList = shopInfoDAO.getShopList(request.getLineId());
        LineInfoDO updateLine = new LineInfoDO();
        updateLine.setId(request.getLineId());
        updateLine.setWantShopNum(shopList.size() + shopNameList.size());
        lineInfoDAO.updateLineInfo(updateLine);
        List<ShopInfoDO> addShopList = new ArrayList<>();
        for (String shopName : shopNameList) {
            ShopInfoDO addShop = new ShopInfoDO();
            addShop.setRegionId(lineInfo.getRegionId());
            addShop.setLineId(request.getLineId());
            addShop.setPartnerId(lineInfo.getPartnerId());
            addShop.setShopName(shopName);
            addShopList.add(addShop);
        }
        shopInfoDAO.batchAddShop(addShopList);
        List<Long> shopIds = addShopList.stream().map(ShopInfoDO::getId).collect(Collectors.toList());
        shopStageInfoDAO.initShopStageInfo(lineInfo.getId(), shopIds);
        return 1L;
    }

    @Override
    public ShopInfoDO getShopInfo(Long shopId) {

        return  shopInfoDAO.getShopInfo(shopId);
    }

    @Override
    public Integer updateShopCode(UpdateShopCodeRequest request) {
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(request.getShopId());
        if(Objects.isNull(shopInfo)){
            throw new ServiceException(ErrorCodeEnum.POINT_NOT_EXIST);
        }
        shopInfo.setShopCode(request.getShopCode());
        shopInfo.setUpdateTime(new Date());
        return   shopInfoDAO.updateShopInfo(shopInfo);
    }
}
