package com.zhongzheng.modules.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.course.domain.MajorCategory;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.zhongzheng.modules.order.bo.CouponAddBo;
import com.zhongzheng.modules.order.bo.CouponQueryBo;
import com.zhongzheng.modules.order.bo.CouponEditBo;
import com.zhongzheng.modules.order.domain.Coupon;
import com.zhongzheng.modules.order.mapper.CouponMapper;
import com.zhongzheng.modules.order.vo.CouponVo;
import com.zhongzheng.modules.order.service.ICouponService;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-15
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Override
    public CouponVo queryById(Long couponId){
        Coupon db = this.baseMapper.selectById(couponId);
        return BeanUtil.toBean(db, CouponVo.class);
    }

    @Override
    public Coupon queryByIdAndUserId(Long couponId, Long userId) {
        Coupon info = getOne(new LambdaQueryWrapper<Coupon>()
                .eq(Coupon::getCouponId,couponId)
                .eq(Coupon::getUserId,userId)
                .last("limit 1"));
        if (Validator.isNotNull(info)) {
            return info;
        }
        return null;
    }

    @Override
    public List<CouponVo> queryList(CouponQueryBo bo) {
        LambdaQueryWrapper<Coupon> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPrice() != null, Coupon::getPrice, bo.getPrice());
        lqw.eq(bo.getEndTime() != null, Coupon::getEndTime, bo.getEndTime());
        lqw.eq(bo.getUserId() != null, Coupon::getUserId, bo.getUserId());
        lqw.eq(bo.getUserStatus() != null, Coupon::getUserStatus, bo.getUserStatus());
        lqw.eq(bo.getConfigurationId() != null, Coupon::getConfigurationId, bo.getConfigurationId());
        lqw.eq(bo.getCouponType() != null, Coupon::getCouponType, bo.getCouponType());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<CouponVo> entity2Vo(Collection<Coupon> collection) {
        List<CouponVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, CouponVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<Coupon> page = (Page<Coupon>)collection;
            Page<CouponVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    public Boolean insertByAddBo(CouponAddBo bo) {
        Coupon add = BeanUtil.toBean(bo, Coupon.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());
        return this.save(add);
    }

    @Override
    public Boolean updateByEditBo(CouponEditBo bo) {
        Coupon update = BeanUtil.toBean(bo, Coupon.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Coupon entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    @Override
    public List<CouponVo> queryIssueListCoupon(CouponQueryBo bo) {
        List<CouponVo> couponVos = baseMapper.queryIssueListCoupon(bo);
        return couponVos;
    }

    @Override
    public Integer selectCount(Long goodsId, Long couponId) {
        return baseMapper.selectCountCon(goodsId,couponId);
    }

    @Override
    public List<CouponVo> listUserOrder(Long userId,Integer couponType) {
        CouponQueryBo couponQueryBo = new CouponQueryBo();
        couponQueryBo.setUserId(userId);
        couponQueryBo.setUserStatus(0);
        couponQueryBo.setCouponType(couponType);
        List<CouponVo> couponVos = baseMapper.queryIssueListCoupon(couponQueryBo);
        return couponVos;
    }
}
