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

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.core.page.PageDomain;
import cn.iocoder.yudao.module.core.page.TableSupport;
import cn.iocoder.yudao.module.shop.entity.ShopConfig;
import cn.iocoder.yudao.module.shop.entity.ShopRepairGoods;
import cn.iocoder.yudao.module.shop.mapper.ShopRepairGoodsMapper;
import cn.iocoder.yudao.module.shop.service.IShopConfigService;
import cn.iocoder.yudao.module.shop.service.IShopRepairGoodsService;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.util.StringUtils;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 汽修商品Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-08
 */
@Service
public class ShopRepairGoodsServiceImpl implements IShopRepairGoodsService
{
    @Autowired
    private ShopRepairGoodsMapper shopRepairGoodsMapper;
    @Autowired
    private IShopConfigService configService;
    @Autowired
    private AdminUserService userService;

    /**
     * 查询汽修商品
     *
     * @param id 汽修商品主键
     * @return 汽修商品
     */
    @Override
    public ShopRepairGoods selectShopRepairGoodsById(Long id)
    {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        AdminUserDO sysUser = userService.getUser(userId);
        Integer userLevel = sysUser.getUserLevel();
        ShopRepairGoods good = shopRepairGoodsMapper.selectShopRepairGoodsById(id);
        //处理图片格式
        if (!StringUtils.isEmpty(good.getImages())){
            good.setImageList(good.getImages().split(","));
        }
        ShopConfig shopConfig = configService.selectShopConfigById(1L);
        //原价单位分
        Long priceFen = good.getPrice();
        Double priceYuan= priceFen.doubleValue()/100d;
        good.setOriginalPrice(BigDecimal.valueOf(priceYuan));
        if (good.getIsSpecial()==1|| ObjectUtils.isEmpty(userLevel)||0==userLevel){
            //直接计算原价
            good.setRealPrice(BigDecimal.valueOf(priceYuan));
        }
        else if (userLevel==1){
            //白银会员
            //获取会员的优惠折扣
            BigDecimal inspectionPlatinum = shopConfig.getRepairSilver();
            Double ratio = inspectionPlatinum.divide(BigDecimal.valueOf(10)).doubleValue();
            good.setRealPrice(BigDecimal.valueOf(priceYuan*ratio).setScale(2,BigDecimal.ROUND_HALF_UP));

        }else if (userLevel==2){
            //黄金会员
            BigDecimal inspectionPlatinum = shopConfig.getRepairGold();
            Double ratio = inspectionPlatinum.divide(BigDecimal.valueOf(10)).doubleValue();
            good.setRealPrice(BigDecimal.valueOf(priceYuan*ratio).setScale(2,BigDecimal.ROUND_HALF_UP));
        }else if (userLevel==3){
            //白金会员
            BigDecimal inspectionPlatinum = shopConfig.getRepairPlatinum();
            Double ratio = inspectionPlatinum.divide(BigDecimal.valueOf(10)).doubleValue();
            good.setRealPrice(BigDecimal.valueOf(priceYuan*ratio).setScale(2,BigDecimal.ROUND_HALF_UP));
        }
        return good;
    }

    /**
     * 查询汽修商品列表
     *
     * @param shopRepairGoods 汽修商品
     * @return 汽修商品
     */
    @Override
    public List<ShopRepairGoods> selectShopRepairGoodsList(ShopRepairGoods shopRepairGoods)
    {
        ShopConfig shopConfig = configService.selectShopConfigById(1L);
        //获取最高会员的优惠折扣
        BigDecimal inspectionPlatinum = shopConfig.getRepairPlatinum();
        Double ratio = inspectionPlatinum.divide(BigDecimal.valueOf(10)).doubleValue();
        PageDomain pageDomain = TableSupport.getPageDomain();
        PageHelper.startPage(pageDomain.getPageNum(), pageDomain.getPageSize(), pageDomain.getOrderBy());
        List<ShopRepairGoods> shopInspectionRes = shopRepairGoodsMapper.selectShopRepairGoodsList(shopRepairGoods);
        for (ShopRepairGoods good : shopInspectionRes) {
            if (good.getIsSpecial().equals(0)){
                //原价单位分
                Long priceFen = good.getPrice();
                Double priceYuan= priceFen.doubleValue()/100d;
                good.setLowPrice(BigDecimal.valueOf(priceYuan*ratio).setScale(2,BigDecimal.ROUND_HALF_UP));
            }
        }
        return shopInspectionRes;
    }

    /**
     * 新增汽修商品
     *
     * @param shopRepairGoods 汽修商品
     * @return 结果
     */
    @Override
    public int insertShopRepairGoods(ShopRepairGoods shopRepairGoods)
    {
        return shopRepairGoodsMapper.insertShopRepairGoods(shopRepairGoods);
    }

    /**
     * 修改汽修商品
     *
     * @param shopRepairGoods 汽修商品
     * @return 结果
     */
    @Override
    public int updateShopRepairGoods(ShopRepairGoods shopRepairGoods)
    {
        return shopRepairGoodsMapper.updateShopRepairGoods(shopRepairGoods);
    }

    /**
     * 批量删除汽修商品
     *
     * @param ids 需要删除的汽修商品主键
     * @return 结果
     */
    @Override
    public int deleteShopRepairGoodsByIds(Long[] ids)
    {
        return shopRepairGoodsMapper.deleteShopRepairGoodsByIds(ids);
    }

    /**
     * 删除汽修商品信息
     *
     * @param id 汽修商品主键
     * @return 结果
     */
    @Override
    public int deleteShopRepairGoodsById(Long id)
    {
        return shopRepairGoodsMapper.deleteShopRepairGoodsById(id);
    }
}
