package com.yiqi.core.service.impl;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.activity.domain.ActivityApplyOrg;
import com.yiqi.activity.domain.ActivityClothItem;
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.enums.UserStatus;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.ClothPrice;
import com.yiqi.core.domain.vo.ClothItemPriceVO;
import com.yiqi.core.domain.vo.ClothItemVo;
import com.yiqi.core.domain.vo.ClothItemWithPriceQueryVO;
import com.yiqi.core.domain.vo.ClothPriceSetVO;
import com.yiqi.core.mapper.ClothPriceMapper;
import com.yiqi.core.service.IClothPriceService;
import com.yiqi.system.domain.SysOrg;
import com.yiqi.system.domain.SysStore;
import com.yiqi.system.service.ISysOrgService;
import com.yiqi.system.service.ISysStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.core.mapper.ClothItemMapper;
import com.yiqi.core.domain.ClothItem;
import com.yiqi.core.service.IClothItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 衣服Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-10
 */
@Service
public class ClothItemServiceImpl extends ServiceImpl<ClothItemMapper, ClothItem> implements IClothItemService {

    @Autowired
    private ISysOrgService sysOrgService;

    @Autowired
    IClothPriceService clothPriceService;

    @Autowired
    ClothPriceMapper clothPriceMapper;

    @Autowired
    IActivityClothItemService activityClothItemService;

    @Autowired
    IActivityInfoService activityInfoService;

    @Autowired
    IActivityApplyOrgService activityApplyOrgService;

    @Autowired
    private ISysStoreService sysStoreService;

    @Override
    public List<ClothItemWithPriceQueryVO> allWithPrice(Long clothTypeId) {
        SysOrg sysOrg = sysOrgService.getOne(new QueryWrapper<SysOrg>().lambda()
                .eq(SysOrg::getSourceType, SecurityUtils.getLoginUser().getSourceType())
                .eq(SysOrg::getId, SecurityUtils.getLoginUser().getOrgId()));
        if (sysOrg == null) {
            throw new ServiceException("未查询到组织信息");
        }
        List<ClothItemWithPriceQueryVO> clothItemWithPriceQueryVOS = baseMapper.allWithPrice(clothTypeId, sysOrg.getBindAreaId());
        if (CollUtil.isEmpty(clothItemWithPriceQueryVOS)) {
            return clothItemWithPriceQueryVOS;
        }
        return clothItemWithPriceQueryVOS;
    }

    @Override
    public List<ClothItemPriceVO> selectClothItemPriceList(Long storeId) {
        return baseMapper.selectClothItemPriceList(storeId);
    }

    /**
     * 查询衣服
     *
     * @param id 衣服主键
     * @return 衣服
     */
    @Override
    public ClothItem selectClothItemById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询洗护列表
     *
     * @param clothItem 衣服
     * @return 衣服
     */
    @Override
    public List<ClothPriceSetVO> selectClothItemPriceList(Long bindAreaId, ClothItem clothItem) {
        List<ClothPriceSetVO> clothItemList = clothPriceService.selectClothPriceSetList(clothItem, bindAreaId);
        List<ClothPrice> typePrice = clothPriceMapper.selectClothPriceGroupByClothType(bindAreaId);
        //按照衣服id 和 类型进行把typePrice分组
        Map<Long, List<ClothPrice>> clothPriceMapById = typePrice.stream().collect(Collectors.groupingBy(ClothPrice::getClothId));
        List<ClothPrice> washPrice = clothPriceMapper.selectClothPriceGroupByWashMode(bindAreaId);
        Map<Long, List<ClothPrice>> clothPriceMapByWash = washPrice.stream().collect(Collectors.groupingBy(ClothPrice::getClothId));
        for (ClothPriceSetVO clothPriceSetVO : clothItemList) {
            List<ClothPrice> washPrices = clothPriceMapByWash.get(clothPriceSetVO.getId());
            if (CollUtil.isNotEmpty(washPrices)) {
                Map<Long, ClothPrice> clothPriceMap = washPrices.stream().collect(Collectors.toMap(ClothPrice::getWashModeId, clothPrice -> clothPrice));
                if (clothPriceMap.get(1L) != null) {
                    clothPriceSetVO.setDryCleaningModePrice(clothPriceMap.get(1L).getPrice());
                }
                if (clothPriceMap.get(2L) != null) {
                    clothPriceSetVO.setWashModePrice(clothPriceMap.get(2L).getPrice());
                }
                if (clothPriceMap.get(4L) != null) {
                    clothPriceSetVO.setDarningModePrice(clothPriceMap.get(4L).getPrice());
                }
                if (clothPriceMap.get(5L) != null) {
                    clothPriceSetVO.setIronModePrice(clothPriceMap.get(5L).getPrice());
                }
                if (clothPriceMap.get(7L) != null) {
                    clothPriceSetVO.setFineWashModePrice(clothPriceMap.get(7L).getPrice());
                }
                if (clothPriceMap.get(8L) != null) {
                    clothPriceSetVO.setNurseModePrice(clothPriceMap.get(8L).getPrice());
                }
                if (clothPriceMap.get(9L) != null) {
                    clothPriceSetVO.setShehuModePrice(clothPriceMap.get(9L).getPrice());
                }
            }

            List<ClothPrice> typePrices = clothPriceMapById.get(clothPriceSetVO.getId());
            if (CollUtil.isNotEmpty(typePrices)) {
                Map<Integer, ClothPrice> clothPriceMap = typePrices.stream().collect(Collectors.toMap(ClothPrice::getClothType, clothPrice -> clothPrice));
                /**
                 *     thickening(1, "加厚"),
                 *     cashmere(2, "羊绒"),
                 *     silk(3, "真丝"),
                 *     child(4, "儿童"),
                 */
                if (clothPriceMap.get(1) != null) {
                    clothPriceSetVO.setPlusPrice(clothPriceMap.get(1).getPrice());
                }
                if (clothPriceMap.get(2) != null) {
                    clothPriceSetVO.setSheepPrice(clothPriceMap.get(2).getPrice());
                }
                if (clothPriceMap.get(3) != null) {
                    clothPriceSetVO.setSilkPrice(clothPriceMap.get(3).getPrice());
                }
                if (clothPriceMap.get(4) != null) {
                    clothPriceSetVO.setChildPrice(clothPriceMap.get(4).getPrice());
                }
            }
        }
        return clothItemList;
    }

    @Override
    public List<ClothPriceSetVO> priceListByWahMode(ClothItem clothItem) {
        return null;
    }


    /**
     * 查询衣服列表
     *
     * @param clothItem 衣服
     * @return 衣服
     */
    @Override
    public List<ClothItemVo> selectClothItemList(ClothItem clothItem) {
        return baseMapper.selectClothItemList(clothItem);
    }

    /**
     * 新增衣服
     *
     * @param clothItem 衣服
     * @return 结果
     */
    @Override
    public int insertClothItem(ClothItem clothItem) {
        clothItem.setCreateTime(DateUtils.getNowDate());
        clothItem.setCreateBy(SecurityUtils.getUsername());
        clothItem.setCreateById(SecurityUtils.getUserId());
        clothItem.setDelFlag(UserStatus.OK.getCode());
        clothItem.setStatus(UserStatus.OK.getCode());
        return baseMapper.insertClothItem(clothItem);
    }

    /**
     * 修改衣服
     *
     * @param clothItem 衣服
     * @return 结果
     */
    @Override
    public int updateClothItem(ClothItem clothItem) {
        clothItem.setUpdateTime(DateUtils.getNowDate());
        clothItem.setUpdateBy(SecurityUtils.getUsername());
        clothItem.setUpdateById(SecurityUtils.getUserId());
        return baseMapper.updateClothItem(clothItem);
    }

    /**
     * 批量删除衣服
     *
     * @param ids 需要删除的衣服主键
     * @return 结果
     */
    @Override
    public int deleteClothItemByIds(Long[] ids) {
        return baseMapper.deleteClothItemByIds(ids, SecurityUtils.getUserId(), SecurityUtils.getUsername());
    }

    /**
     * 删除衣服信息
     *
     * @param id 衣服主键
     * @return 结果
     */
    @Override
    public int deleteClothItemById(Long id) {
        return baseMapper.deleteClothItemById(id, SecurityUtils.getUserId(), SecurityUtils.getUsername());
    }

    @Override
    public int updateClothItemStatus(ClothItem clothItem) {
        clothItem.setUpdateTime(DateUtils.getNowDate());
        clothItem.setUpdateBy(SecurityUtils.getUsername());
        clothItem.setUpdateById(SecurityUtils.getUserId());
        return baseMapper.updateClothItem(clothItem);
    }
}
