package com.cool.store.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReflectUtil;
import com.cool.store.constants.CommonConstants;
import com.cool.store.dao.*;
import com.cool.store.entity.OpenAcceptanceInfoDO;
import com.cool.store.entity.ShopInfoDO;
import com.cool.store.entity.ShopStageInfoDO;
import com.cool.store.enums.point.ShopStageEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageFieldEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.request.OpenAcceptanceRequest;
import com.cool.store.request.ShopAcceptanceRequest;
import com.cool.store.request.ShopStageAcceptanceRequest;
import com.cool.store.service.OpenAcceptanceInfoService;
import com.cool.store.service.RegionService;
import com.cool.store.utils.CoolDateUtils;
import com.cool.store.utils.poi.DateUtils;
import com.cool.store.vo.EmployeeTrainingVO;
import com.cool.store.vo.OpenAcceptanceInfoListVO;
import com.cool.store.vo.ShopAcceptanceVO;
import com.cool.store.vo.ShopStageAcceptanceVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

/**
 * @author byd
 * @date 2024-04-22 14:41
 */
@Service
public class OpenAcceptanceInfoServiceImpl implements OpenAcceptanceInfoService {

    @Resource
    private OpenAcceptanceInfoDAO openAcceptanceInfoDAO;

    @Resource
    private EnterpriseUserDAO userDAO;

    @Resource
    private RegionService regionService;

    @Resource
    private RegionDao regionDao;

    @Resource
    private ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    ShopInfoDAO shopInfoDAO;

    @Override
    public PageInfo<OpenAcceptanceInfoListVO> openAcceptanceList(OpenAcceptanceRequest openAcceptanceRequest) {
        PageHelper.startPage(openAcceptanceRequest.getPageNum(), openAcceptanceRequest.getPageSize());
        List<OpenAcceptanceInfoListVO> resultList = openAcceptanceInfoDAO.openAcceptanceList(openAcceptanceRequest);
        List<String> userIdList = new ArrayList<>();
        for (OpenAcceptanceInfoListVO vo : resultList) {
            userIdList.add(vo.getSupervisorUserId());
            userIdList.add(vo.getShopManagerUserId());
            userIdList.add(vo.getAcceptanceUserId());
            userIdList.add(vo.getInvestmentManager());
        }

        List<Long> regionIds = resultList.stream().map(OpenAcceptanceInfoListVO::getRegionId).distinct().collect(Collectors.toList());

        Map<Long, String> regionNameMap = regionService.getBelongWarRegionNameMap(regionIds);
        if (CollectionUtils.isNotEmpty(userIdList)) {
            Map<String, String> userNameMap = userDAO.getUserNameMap(userIdList);
            for (OpenAcceptanceInfoListVO vo : resultList) {
                vo.setSupervisorUserName(userNameMap.get(vo.getSupervisorUserId()));
                vo.setShopManagerUserName(userNameMap.get(vo.getShopManagerUserId()));
                vo.setAcceptanceUserName(userNameMap.get(vo.getAcceptanceUserId()));
                vo.setInvestmentManagerName(userNameMap.get(vo.getInvestmentManager()));
                if (vo.getPlanOpenTime() != null) {
                    vo.setPlanOpenEndTime(DateUtils.addDays(vo.getPlanOpenTime(), CommonConstants.THREE));
                }
                if (vo.getRegionId() != null) {
                    vo.setRegionId(vo.getRegionId());
                    vo.setRegionNodeName(regionNameMap.get(vo.getRegionId()));
                }
            }
        }
        return new PageInfo<>(resultList);
    }

    @Override
    public ShopAcceptanceVO acceptanceStageList(Long shopId) {
        List<ShopStageInfoDO> stageInfoDOList = shopStageInfoDAO.getShopStageInfo(shopId, ShopStageEnum.SHOP_STAGE_2.getShopStage());
        List<ShopStageAcceptanceVO> result = new ArrayList<>();
        OpenAcceptanceInfoDO openAcceptanceInfoDO = openAcceptanceInfoDAO.selectOne(OpenAcceptanceInfoDO.builder().shopId(shopId).build());
        stageInfoDOList.forEach(stageInfoDO -> {
            ShopStageAcceptanceVO shopStageAcceptanceVO = new ShopStageAcceptanceVO();
            shopStageAcceptanceVO.setShopId(stageInfoDO.getShopId());
            shopStageAcceptanceVO.setShopSubStage(stageInfoDO.getShopSubStage());
            shopStageAcceptanceVO.setId(stageInfoDO.getId());
            shopStageAcceptanceVO.setActualCompleteTime(stageInfoDO.getActualCompleteTime());
            ShopSubStageEnum shopSubStageEnum = ShopSubStageEnum.getByShopSubStage(stageInfoDO.getShopSubStage());
            if (shopSubStageEnum != null) {
                shopStageAcceptanceVO.setShopSubStageName(shopSubStageEnum.getShopSubStageName());
            }
            ShopSubStageFieldEnum shopSubStageFieldEnum = ShopSubStageFieldEnum.getByShopSubStage(stageInfoDO.getShopSubStage());
            if (shopSubStageFieldEnum != null) {
                shopStageAcceptanceVO.setResult(ReflectUtil.getFieldValue(openAcceptanceInfoDO, shopSubStageFieldEnum.getFiled()) == null ?
                        null : (Boolean) ReflectUtil.getFieldValue(openAcceptanceInfoDO, shopSubStageFieldEnum.getFiled()));
            }
            result.add(shopStageAcceptanceVO);
        });
        ShopAcceptanceVO shopAcceptanceVO = new ShopAcceptanceVO();
        shopAcceptanceVO.setShopStageAcceptanceList(result);
        shopAcceptanceVO.setShopId(shopId);
        shopAcceptanceVO.setPlanOpenTime(openAcceptanceInfoDO.getPlanOpenTime());
        shopAcceptanceVO.setAcceptanceStatus(openAcceptanceInfoDO.getAcceptanceStatus());
        return shopAcceptanceVO;
    }

    @Override
    public void acceptance(ShopAcceptanceRequest shopAcceptanceRequest, String userId) {
        OpenAcceptanceInfoDO openAcceptanceInfoDO = openAcceptanceInfoDAO.selectOne(OpenAcceptanceInfoDO.builder().shopId(shopAcceptanceRequest.getShopId()).build());
        if(openAcceptanceInfoDO == null){
            throw new ServiceException("该店铺不存在");
        }
        if(openAcceptanceInfoDO.getAcceptanceStatus() != null && openAcceptanceInfoDO.getAcceptanceStatus() == 1){
            throw new ServiceException("该店铺已验收完成，不能再次验收");
        }
        shopAcceptanceRequest.getShopStageAcceptanceList().forEach(shopStageAcceptanceRequest -> {
            ShopSubStageFieldEnum shopSubStageFieldEnum = ShopSubStageFieldEnum.getByShopSubStage(shopStageAcceptanceRequest.getShopSubStage());
            if (shopSubStageFieldEnum != null) {
                ReflectUtil.setFieldValue(openAcceptanceInfoDO, shopSubStageFieldEnum.getFiled(), shopStageAcceptanceRequest.getResult());
            }
        });
        openAcceptanceInfoDO.setAcceptanceStatus(1);
        openAcceptanceInfoDO.setAcceptanceUserId(userId);
        openAcceptanceInfoDO.setPlanOpenTime(shopAcceptanceRequest.getPlanOpenTime());
        openAcceptanceInfoDO.setAcceptanceTime(new Date());
        openAcceptanceInfoDAO.updateByPrimaryKeySelective(openAcceptanceInfoDO);
        ShopInfoDO shopInfoDO = new ShopInfoDO();
        shopInfoDO.setId(shopAcceptanceRequest.getShopId());
        shopInfoDO.setPlanOpenTime(shopAcceptanceRequest.getPlanOpenTime());
        shopInfoDAO.updateShopInfo(shopInfoDO);
        //开业验收完成
//        shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_161);
        //新店开业试运营
        shopStageInfoDAO.updateShopStageInfo(shopInfoDO.getId(), ShopSubStageStatusEnum.SHOP_SUB_STAGE_STATUS_170);
    }

    @Override
    public void acceptanceOne(ShopStageAcceptanceRequest shopStageAcceptanceRequest) {
        OpenAcceptanceInfoDO openAcceptanceInfoDO = openAcceptanceInfoDAO.selectOne(OpenAcceptanceInfoDO.builder().shopId(shopStageAcceptanceRequest.getShopId()).build());
        if (openAcceptanceInfoDO == null) {
            throw new ServiceException("该店铺不存在");
        }
        if (openAcceptanceInfoDO.getAcceptanceStatus() != null && openAcceptanceInfoDO.getAcceptanceStatus() == 1) {
            throw new ServiceException("该店铺已验收完成，不能再次验收");
        }

        ShopSubStageFieldEnum shopSubStageFieldEnum = ShopSubStageFieldEnum.getByShopSubStage(shopStageAcceptanceRequest.getShopSubStage());
        if (shopSubStageFieldEnum != null) {
            ReflectUtil.setFieldValue(openAcceptanceInfoDO, shopSubStageFieldEnum.getFiled(), shopStageAcceptanceRequest.getResult());
        }
        openAcceptanceInfoDAO.updateByPrimaryKeySelective(openAcceptanceInfoDO);
    }
}
