package cn.iocoder.yudao.module.member.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.member.entity.MemberCoupon;
import cn.iocoder.yudao.module.member.mapper.MemberCouponMapper;
import cn.iocoder.yudao.module.member.service.MemberCouponService;
import cn.iocoder.yudao.module.member.vo.MemberCouponPageReqVO;
import cn.iocoder.yudao.module.member.vo.MemberCouponRespVO;
import cn.iocoder.yudao.module.member.vo.MemberCouponSaveReqVO;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 基础卡券 Service 实现类
 *
 * @author pqz
 */
@Service
@Validated
public class MemberCouponServiceImpl extends ServiceImpl<MemberCouponMapper, MemberCoupon> implements MemberCouponService {

    @Resource
    private MemberCouponMapper couponMapper;


    /**
     * 保存基础卡券信息
     *
     * @param saveReqVO MemberCouponSaveReqVO实体
     * @return void
     * @author PQZ
     * @date 14:22 2024/9/19
     **/
    @Override
    public void saveCoupon(MemberCouponSaveReqVO saveReqVO) {
        MemberCoupon memberCoupon = BeanUtils.toBean(saveReqVO, MemberCoupon.class);
        saveOrUpdate(memberCoupon);
    }

    /**
     * 通过id集合查询卡券信息
     *
     * @param ids     id集合
     * @param isChild 是否查询出关联的卡券信息
     * @return java.util.List<cn.iocoder.yudao.module.member.entity.MemberCoupon>
     * @author PQZ
     * @date 10:47 2024/9/21
     **/
    @Override
    public List<MemberCoupon> listByIds(List<String> ids, boolean isChild) {
        List<MemberCoupon> couponList = new ArrayList<>();
        if (!ids.isEmpty()) {
            LambdaQueryWrapper<MemberCoupon> couponLambdaQueryWrapper = new LambdaQueryWrapper<>();
            couponLambdaQueryWrapper.in(MemberCoupon::getId, ids).eq(MemberCoupon::getDeleted, "0");
            couponList = list(couponLambdaQueryWrapper);
        }
        List<MemberCoupon> result = new ArrayList<>(couponList);
        if (isChild){
            //查询附属卡片
            List<String> parentIds = couponList.stream().map(MemberCoupon::getId).collect(Collectors.toList());
            LambdaQueryWrapper<MemberCoupon> couponLambdaQueryWrapper = new LambdaQueryWrapper<>();
            couponLambdaQueryWrapper.in(MemberCoupon::getParentId, parentIds).eq(MemberCoupon::getDeleted, "0");
            List<MemberCoupon> childList = list(couponLambdaQueryWrapper);
            result.addAll(childList);
        }
        return result;
    }

    @Override
    public void deleteCoupon(String id) {
        // 删除
        couponMapper.deleteById(id);
    }

    /**
     * 查询基础卡券及附属信息
     * @author PQZ
     * @date 21:55 2024/9/19
     * @param id 卡券id
     * @return cn.iocoder.yudao.module.member.vo.MemberCouponRespVO
     **/
    @Override
    public MemberCouponRespVO getCoupon(String id) {
        //查询本身数据
        MemberCoupon memberCoupon = getById(id);
        MemberCouponRespVO result = BeanUtils.toBean(memberCoupon,MemberCouponRespVO.class);
        //查询附属权益
        LambdaQueryWrapper<MemberCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MemberCoupon::getParentId,id).eq(BaseDO::getDeleted,'0');
        List<MemberCoupon> list = list(lambdaQueryWrapper);
        result.setCouponList(list);
        return result;
    }

    /**
     * 分页查询基础卡券信息
     *
     * @param pageReqVO MemberCouponPageReqVO实体
     * @param page      分页参数
     * @return com.baomidou.mybatisplus.core.metadata.IPage<cn.iocoder.yudao.module.member.vo.MemberCouponRespVO>
     * @author PQZ
     * @date 14:22 2024/9/19
     **/
    @Override
    public IPage<MemberCouponRespVO> queryListPage(MemberCouponPageReqVO pageReqVO, Page<MemberCouponRespVO> page) {
        return couponMapper.selectListPage(pageReqVO, page);
    }

    /**
     * 复制卡券附属权益
     *
     * @param saveReqVO MemberCouponSaveReqVO
     * @author PQZ
     * @date 21:25 2024/9/19
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copyMemberCoupon(MemberCouponSaveReqVO saveReqVO) {
        //原始数据
        LambdaQueryWrapper<MemberCoupon> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(MemberCoupon::getParentId, saveReqVO.getId()).eq(BaseDO::getDeleted, '0');
        List<MemberCoupon> oldList = list(lambdaQueryWrapper);
        //组装复制数据
        if (!oldList.isEmpty()) {
            //要保存的数据集合
            List<MemberCoupon> saveList = new ArrayList<>();
            //选中的目标数据
            List<MemberCoupon> targetList = saveReqVO.getCouponList();
            if (targetList != null && !targetList.isEmpty()) {
                targetList.forEach(targetItem -> {
                    //深拷贝
                    MemberCoupon target = BeanUtils.toBean(targetItem,MemberCoupon.class);
                    oldList.forEach(oldItem -> {
                        //深拷贝
                        MemberCoupon saveItem = BeanUtils.toBean(oldItem,MemberCoupon.class);
                        saveItem.setId(null);
                        saveItem.setParentId(target.getId());
                        saveItem.setCreateTime(null);
                        saveItem.setCreator(null);
                        saveList.add(saveItem);
                    });
                });
            }
            saveBatch(saveList);
        }
    }


}