package cn.ysk.cashier.mybatis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapProxy;
import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.entity.TbPointsGoodsSetting;
import cn.ysk.cashier.mybatis.mapper.TbPointsGoodsSettingMapper;
import cn.ysk.cashier.mybatis.service.TbPointsGoodsSettingService;
import cn.ysk.cashier.utils.PageUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 积分商品设置
 *
 * @author Tankaikai tankaikai@aliyun.com
 * @since 2.0 2024-10-25
 */
@Service
public class TbPointsGoodsSettingServiceImpl extends ServiceImpl<TbPointsGoodsSettingMapper, TbPointsGoodsSetting> implements TbPointsGoodsSettingService {


    @Override
    public Map<String, Object> page(Map<String, Object> params) {
        MapProxy mapProxy = MapProxy.create(params);
        int pageNum = mapProxy.getInt("page", 1);
        int pageSize = mapProxy.getInt("size", 10);
        TbPointsGoodsSetting param = BeanUtil.toBean(params, TbPointsGoodsSetting.class);
        Page<TbPointsGoodsSetting> pg = new Page<>(pageNum, pageSize);
        pg.addOrder(OrderItem.desc("sort"));
        Page<TbPointsGoodsSetting> page = baseMapper.selectPage(pg,
                Wrappers.<TbPointsGoodsSetting>lambdaQuery()
                        .eq(TbPointsGoodsSetting::getShopId, param.getShopId())
                        .like(StrUtil.isNotEmpty(param.getGoodsName()), TbPointsGoodsSetting::getGoodsName, param.getGoodsName())
                        .like(StrUtil.isNotEmpty(param.getGoodsCategory()), TbPointsGoodsSetting::getGoodsCategory, param.getGoodsCategory())
                        .eq(param.getStatus() != null, TbPointsGoodsSetting::getStatus, param.getStatus())
                        .eq(TbPointsGoodsSetting::getDelFlag, 0)
                        .orderByDesc(TbPointsGoodsSetting::getId)
        );
        return PageUtil.toPlusPage(page.getRecords(), Convert.toInt(page.getTotal()));
    }

    @Override
    public boolean save(TbPointsGoodsSetting entity) {
        try {
            Assert.notNull(entity.getShopId(), "{}({})不能为空", "店铺id", "shopId");
            Assert.notEmpty(entity.getGoodsCategory(), "{}({})不能为空", "商品类型", "goodsCategory");
            Assert.notEmpty(entity.getGoodsName(), "{}({})不能为空", "商品名称", "goodsName");
            //Assert.notNull(entity.getGoodsImageUrl(), "{}({})不能为空", "商品图片URL","goodsImageUrl");
            Assert.notNull(entity.getRequiredPoints(), "{}({})不能为空", "所需积分", "requiredPoints");
            Assert.notNull(entity.getExtraPrice(), "{}({})不能为空", "额外价格", "extraPrice");
            Assert.notNull(entity.getSort(), "{}({})不能为空", "排序(权重)", "sort");
            Assert.notNull(entity.getQuantity(), "{}({})不能为空", "数量", "quantity");
            //Assert.notEmpty(entity.getGoodsDescription(), "{}({})不能为空", "商品详情","goodsDescription");
            Assert.notNull(entity.getStatus(), "{}({})不能为空", "是否上架", "status");
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(e.getMessage());
        }
        entity.setCreateTime(new Date());
        return super.save(entity);
    }

    @Override
    public boolean update(TbPointsGoodsSetting dto) {
        try {
            Assert.notNull(dto.getId(), "{}不能为空", "商品id");
            Assert.notNull(dto.getShopId(), "{}({})不能为空", "店铺id", "shopId");
            Assert.notEmpty(dto.getGoodsCategory(), "{}({})不能为空", "商品类型", "goodsCategory");
            Assert.notEmpty(dto.getGoodsName(), "{}({})不能为空", "商品名称", "goodsName");
            //Assert.notNull(entity.getGoodsImageUrl(), "{}({})不能为空", "商品图片URL","goodsImageUrl");
            Assert.notNull(dto.getRequiredPoints(), "{}({})不能为空", "所需积分", "requiredPoints");
            Assert.notNull(dto.getExtraPrice(), "{}({})不能为空", "额外价格", "extraPrice");
            Assert.notNull(dto.getSort(), "{}({})不能为空", "排序(权重)", "sort");
            Assert.notNull(dto.getQuantity(), "{}({})不能为空", "数量", "quantity");
            //Assert.notEmpty(entity.getGoodsDescription(), "{}({})不能为空", "商品详情","goodsDescription");
            Assert.notNull(dto.getStatus(), "{}({})不能为空", "是否上架", "status");
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(e.getMessage());
        }
        TbPointsGoodsSetting entity = super.getById(dto.getId());
        BeanUtil.copyProperties(dto, entity, CopyOptions.create().setIgnoreNullValue(false).setIgnoreProperties("createTime"));
        entity.setUpdateTime(new Date());
        return super.updateById(entity);
    }

    @Override
    public void delete(Long id) {
        super.update(Wrappers.<TbPointsGoodsSetting>lambdaUpdate().set(TbPointsGoodsSetting::getDelFlag, 1).eq(TbPointsGoodsSetting::getId, id));
    }
}