package com.yiqi.app.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.app.domain.dto.AppCouponTypeApplyClothDTO;
import com.yiqi.app.domain.vo.AppCouponTypeApplyClothVO;
import com.yiqi.common.enums.DeleteStatus;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.ClothItem;
import com.yiqi.core.domain.dto.ClothItemDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.app.mapper.AppCouponTypeApplyClothMapper;
import com.yiqi.app.domain.AppCouponTypeApplyCloth;
import com.yiqi.app.service.IAppCouponTypeApplyClothService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 优惠券可使用衣物Service业务层处理
 *
 * @author xiaohei
 * @date 2023-09-20
 */
@Service
public class AppCouponTypeApplyClothServiceImpl extends ServiceImpl<AppCouponTypeApplyClothMapper,AppCouponTypeApplyCloth> implements IAppCouponTypeApplyClothService {

    /**
     * 查询优惠券可使用衣物
     *
     * @param id 优惠券可使用衣物主键
     * @return 优惠券可使用衣物
     */
    @Override
    public AppCouponTypeApplyCloth selectAppCouponTypeApplyClothById(Long id) {
        return baseMapper.selectAppCouponTypeApplyClothById(id);
    }

    /**
     * 查询优惠券可使用衣物列表
     *
     * @param clothItemDTO 衣物
     * @return 优惠券可使用衣物
     */
    @Override
    public List<AppCouponTypeApplyClothVO> selectAppCouponTypeApplyClothList(ClothItemDTO clothItemDTO) {
        return baseMapper.selectAppCouponTypeApplyClothList(clothItemDTO);
    }

    /**
     * 新增优惠券可使用衣物
     *
     * @param appCouponTypeApplyClothDTO 优惠券可使用衣物
     * @return 结果
     */
    @Override
    public int insertAppCouponTypeApplyCloth(AppCouponTypeApplyClothDTO appCouponTypeApplyClothDTO) {
        List<AppCouponTypeApplyCloth> list = this.list(new QueryWrapper<AppCouponTypeApplyCloth>().lambda()
                .eq(AppCouponTypeApplyCloth::getCouponTypeId, appCouponTypeApplyClothDTO.getCouponTypeId())
                .eq(AppCouponTypeApplyCloth::getDelFlag, DeleteStatus.OK.getCode()));
        // 已选中的 衣服id
        List<Long> clothItemIdList = list.stream()
                .map(AppCouponTypeApplyCloth::getClothItemId).collect(Collectors.toList());
        // 过滤已存在的id
        List<Long> collect = null;
        if (CollUtil.isNotEmpty(clothItemIdList)){
            collect = appCouponTypeApplyClothDTO.getClothItemIdList().stream()
                    .filter(itemId -> !clothItemIdList.contains(itemId)).collect(Collectors.toList());
        }else {
            collect = appCouponTypeApplyClothDTO.getClothItemIdList();
        }
        List<AppCouponTypeApplyCloth> applyClothArrayList = new ArrayList<>();
        for (Long clothItemId : collect) {
            AppCouponTypeApplyCloth appCouponTypeApplyCloth = new AppCouponTypeApplyCloth();
            appCouponTypeApplyCloth.buildCreateData();
            appCouponTypeApplyCloth.setCouponTypeId(appCouponTypeApplyClothDTO.getCouponTypeId());
            appCouponTypeApplyCloth.setClothItemId(clothItemId);
            applyClothArrayList.add(appCouponTypeApplyCloth);
        }
        this.saveBatch(applyClothArrayList);
        return 1;
    }

    /**
     * 修改优惠券可使用衣物
     *
     * @param appCouponTypeApplyCloth 优惠券可使用衣物
     * @return 结果
     */
    @Override
    public int updateAppCouponTypeApplyCloth(AppCouponTypeApplyCloth appCouponTypeApplyCloth) {
        appCouponTypeApplyCloth.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateAppCouponTypeApplyCloth(appCouponTypeApplyCloth);
    }

    /**
     * 批量删除优惠券可使用衣物
     *
     * @param ids 需要删除的优惠券可使用衣物主键
     * @return 结果
     */
    @Override
    public int deleteAppCouponTypeApplyClothByIds(Long[] ids) {
        return baseMapper.deleteAppCouponTypeApplyClothByIds(ids);
    }

    /**
     * 删除优惠券可使用衣物信息
     *
     * @param id 优惠券可使用衣物主键
     * @return 结果
     */
    @Override
    public int deleteAppCouponTypeApplyClothById(Long id) {
        return baseMapper.deleteAppCouponTypeApplyClothById(id);
    }



}
