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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.exception.CustomException;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.base.bo.ShoppingCartAddBo;
import com.zhongzheng.modules.base.bo.ShoppingCartEditBo;
import com.zhongzheng.modules.base.bo.ShoppingCartQueryBo;
import com.zhongzheng.modules.base.domain.ApplyAreas;
import com.zhongzheng.modules.base.domain.ShoppingCart;
import com.zhongzheng.modules.base.mapper.ApplyAreasMapper;
import com.zhongzheng.modules.base.mapper.ShoppingCartMapper;
import com.zhongzheng.modules.base.service.IShoppingCartService;
import com.zhongzheng.modules.base.vo.ShoppingCartVo;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 购物车Service业务层处理
 *
 * @author hjl
 * @date 2021-12-09
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCartVo queryById(Long id){
        ShoppingCart db = this.baseMapper.selectById(id);
        return BeanUtil.toBean(db, ShoppingCartVo.class);
    }

    @Override
    public List<ShoppingCartVo> selectList(ShoppingCartQueryBo bo) {
        return shoppingCartMapper.selectList(bo);
    }

    @Override
    public List<ShoppingCartVo> queryList(ShoppingCartQueryBo bo) {
        LambdaQueryWrapper<ShoppingCart> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGoodsId() != null, ShoppingCart::getGoodsId, bo.getGoodsId());
        lqw.eq(bo.getUserId() != null, ShoppingCart::getUserId, bo.getUserId());
        lqw.eq(bo.getStatus() != null, ShoppingCart::getStatus, bo.getStatus());
        return entity2Vo(this.list(lqw));
    }

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

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

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

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ShoppingCart entity){
        //TODO 做一些数据校验,如唯一约束
        if(checkUnique(entity)){
            throw new CustomException("该商品已加入购物车");
        }
    }

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

    @Override
    public Boolean deleteById(Long id, Long userId) {
        return this.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,userId).eq(ShoppingCart::getId,id));

    }

    @Override
    public Boolean deleteByGoodsId(Long goodsId, Long userId) {
        return this.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,userId).eq(ShoppingCart::getGoodsId,goodsId));
    }

    private boolean checkUnique(ShoppingCart entity) {
        ShoppingCart info = getOne(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,entity.getUserId()).eq(ShoppingCart::getGoodsId,entity.getGoodsId()).last("limit 1"));
        if (Validator.isNotNull(info)) {
            return true;
        }
        return false;
    }
}
