package com.cool.store.service.impl;

import com.cool.store.constants.CommonConstants;
import com.cool.store.context.LoginUserInfo;
import com.cool.store.dao.*;
import com.cool.store.dto.decoration.ConstructionScheduleDTO;
import com.cool.store.dto.decoration.DecorationDTO;
import com.cool.store.dto.decoration.ProjectDTO;
import com.cool.store.entity.*;
import com.cool.store.enums.AuditResultTypeEnum;
import com.cool.store.enums.AuditTypeEnum;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.UserRoleEnum;
import com.cool.store.enums.point.ShopSubStageEnum;
import com.cool.store.enums.point.ShopSubStageStatusEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.LineInfoMapper;
import com.cool.store.mapper.ShopInfoMapper;
import com.cool.store.request.AcceptanceListRequest;
import com.cool.store.request.SeeAcceptanceRequest;
import com.cool.store.request.SeeAccetanceListRequest;
import com.cool.store.service.*;
import com.cool.store.utils.poi.StringUtils;
import com.cool.store.vo.fitmentCheckVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;

/**
 * @Auther: WangShuo
 * @Date: 2024/05/17/下午5:58
 * @Version 1.0
 * @注释:
 */
@Service
public class SeeAcceptanceServiceImpl implements SeeAcceptanceService {
    private static final Logger log = LoggerFactory.getLogger(SeeAcceptanceServiceImpl.class);
    @Resource
    private DecorationService decorationService;
    @Resource
    private ShopInfoDAO shopInfoDAO;
    @Resource
    private YlfService ylfService;
    @Resource
    private ShopAuditInfoDAO shopAuditInfoDAO;
    @Resource
    private TempUserDetailDAO tempUserDetailDAO;
    @Resource
    private ShopStageInfoDAO shopStageInfoDAO;
    @Resource
    private PreparationService preparationService;
    @Resource
    private UserAuthMappingService userAuthMappingService;
    @Resource
    private ShopInfoMapper shopInfoMapper;
    @Resource
    private LineInfoMapper lineInfoMapper;
    @Resource
    private EnterpriseUserDAO enterpriseUserDAO;
    @Resource
    private RegionService regionService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private AcceptanceInfoDAO acceptanceInfoDAO;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean auditSeeAcceptance(SeeAcceptanceRequest request, LoginUserInfo user) {
        return true;
    }

    @Override
    public ConstructionScheduleDTO getWithdrawal(Long shopId) {
        DecorationDTO decoration = getDecorationDTO(shopId);
        if (decoration == null) {
            log.error("DecorationModel  is null");
            return null;
        }
        ConstructionScheduleDTO constructionScheduleDTO = new ConstructionScheduleDTO();
        List<ConstructionScheduleDTO> constructionSchedule = decoration.getConstructionSchedule();
        for (ConstructionScheduleDTO a : constructionSchedule) {
            if (CommonConstants.WITHDRAWAL.equals(a.getName())) {
                constructionScheduleDTO = a;
            }
        }
        if (Objects.isNull(constructionScheduleDTO)) {
            throw new ServiceException(ErrorCodeEnum.SEE_ACCEPTANCE_ERROR);
        }
        return constructionScheduleDTO;

    }

    @Override
    public List<ShopAuditInfoDO> getAuditInfo(Long shopId) {
        return shopAuditInfoDAO.getAuditList(shopId, AuditTypeEnum.VISUAL_ACCEPTANCE.getCode());
    }

    @Override
    public PageInfo<fitmentCheckVO> getAcceptanceList(AcceptanceListRequest request, LoginUserInfo user) {
        return null;
    }



    private DecorationDTO getDecorationDTO(Long shopId) {
        ShopInfoDO shopInfo = shopInfoDAO.getShopInfo(shopId);
        if (Objects.isNull(shopInfo)) {
            throw new ServiceException(ErrorCodeEnum.SHOP_ID_NOT_EXIST);
        }
        String storeNum = shopInfo.getStoreNum();
        if (StringUtils.isNotEmpty(storeNum)) {
            ProjectDTO projectList = ylfService.getProjectList(storeNum);
            Long projectId = projectList.getProjectId();
            DecorationDTO decoration = ylfService.getDecoration(projectId);
            return decoration;
        }
        return null;
    }
}
