package com.yiqi.core.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.activity.domain.ActivityApplyOrg;
import com.yiqi.activity.domain.vo.ActivityClothItemMinPriceVO;
import com.yiqi.activity.service.IActivityApplyOrgService;
import com.yiqi.activity.service.IActivityClothItemService;
import com.yiqi.activity.service.IActivityInfoService;
import com.yiqi.common.enums.StatusType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.vo.ClothItemWithPriceQueryVO;
import com.yiqi.core.domain.vo.ClothPriceVO;
import com.yiqi.system.domain.SysOrg;
import com.yiqi.system.service.ISysOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothWashModeRelationMapper;
import com.yiqi.core.domain.ClothWashModeRelation;
import com.yiqi.core.service.IClothWashModeRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 洗衣方式和衣服类别关联Service业务层处理
 *
 * @author xiaohei
 * @date 2023-10-11
 */
@Service
public class ClothWashModeRelationServiceImpl extends ServiceImpl<ClothWashModeRelationMapper,ClothWashModeRelation> implements IClothWashModeRelationService {
    @Autowired
    IActivityClothItemService activityClothItemService;
    @Autowired
    private ISysOrgService sysOrgService;
    @Autowired
    IActivityInfoService activityInfoService;
    @Autowired
    IActivityApplyOrgService activityApplyOrgService;
    @Override
    public List<ClothPriceVO> selectWashModePriceByClothId(Long clothId) {
        SysOrg sysOrg = sysOrgService.getOne(new QueryWrapper<SysOrg>().lambda()
                .eq(SysOrg::getSourceType,SecurityUtils.getLoginUser().getSourceType())
                .eq(SysOrg::getId,SecurityUtils.getLoginUser().getOrgId())
                .eq(SysOrg::getStatus, StatusType.OK.getCode()));
        if(sysOrg == null){
            throw new ServiceException("未查询到组织");
        }
        //获取活动信息


        List<ClothPriceVO> clothPriceVOList = baseMapper.selectWashModePriceByClothId(clothId, sysOrg.getBindAreaId());
        // 获取衣服各种洗衣方式最小的指定价格
//        List<ActivityClothItemMinPriceVO> minPriceByActivity = activityClothItemService.getMinPriceWith1ClothItemByActivity(clothId);
//        if (CollUtil.isNotEmpty(minPriceByActivity)){
//            Map<Long, BigDecimal> minPriceByActivityMap = minPriceByActivity.stream()
//                    .collect(Collectors.
//                            toMap(ActivityClothItemMinPriceVO::getWashModeId, ActivityClothItemMinPriceVO::getMinPrice));
//            for (ClothPriceVO clothPriceVO : clothPriceVOList) {
//                if (minPriceByActivityMap.containsKey(clothPriceVO.getWashModeId())){
//                    clothPriceVO.setPrice(minPriceByActivityMap.get(clothPriceVO.getWashModeId()));
//                }
//            }
//        }
        return clothPriceVOList;
    }

    /**
     * 查询洗衣方式和衣服类别关联
     *
     * @param washModeId 洗衣方式和衣服类别关联主键
     * @return 洗衣方式和衣服类别关联
     */
    @Override
    public Set<Long> selectClothWashModeRelationById(Long washModeId) {
        List<ClothWashModeRelation> relations = baseMapper.selectList(new QueryWrapper<ClothWashModeRelation>().lambda()
                .eq(ClothWashModeRelation::getWashModeId, washModeId));
        if (CollUtil.isEmpty(relations)){
            //返回空集合
            return CollUtil.newHashSet();
        }
        // 获取关联的id集合，并将id转换成字符串
        Set<Long> clothTypeIds = relations.stream().map(ClothWashModeRelation::getClothTypeId).collect(Collectors.toSet());
        return clothTypeIds;
    }

    /**
     * 查询洗衣方式和衣服类别关联列表
     *
     * @param clothWashModeRelation 洗衣方式和衣服类别关联
     * @return 洗衣方式和衣服类别关联
     */
    @Override
    public List<ClothWashModeRelation> selectClothWashModeRelationList(ClothWashModeRelation clothWashModeRelation) {
        return baseMapper.selectClothWashModeRelationList(clothWashModeRelation);
    }

    /**
     * 新增洗衣方式和衣服类别关联
     *
     * @param clothWashModeRelation 洗衣方式和衣服类别关联
     * @return 结果
     */
    @Override
    public int insertClothWashModeRelation(ClothWashModeRelation clothWashModeRelation) {
        clothWashModeRelation.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertClothWashModeRelation(clothWashModeRelation);
    }

    /**
     * 修改洗衣方式和衣服类别关联
     *
     * @param clothWashModeRelation 洗衣方式和衣服类别关联
     * @return 结果
     */
    @Override
    public int updateClothWashModeRelation(ClothWashModeRelation clothWashModeRelation) {
        clothWashModeRelation.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateClothWashModeRelation(clothWashModeRelation);
    }

    /**
     * 批量删除洗衣方式和衣服类别关联
     *
     * @param ids 需要删除的洗衣方式和衣服类别关联主键
     * @return 结果
     */
    @Override
    public int deleteClothWashModeRelationByIds(Long[] ids) {
        return baseMapper.deleteClothWashModeRelationByIds(ids);
    }

    /**
     * 删除洗衣方式和衣服类别关联信息
     *
     * @param id 洗衣方式和衣服类别关联主键
     * @return 结果
     */
    @Override
    public int deleteClothWashModeRelationById(Long id) {
        return baseMapper.deleteClothWashModeRelationById(id);
    }



}
