package com.ruoyi.mall.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.ProductCategory;
import com.ruoyi.mall.domain.ProductShow;
import com.ruoyi.mall.domain.vo.ProductShowVO;
import com.ruoyi.mall.mapper.ProductCategoryMapper;
import com.ruoyi.mall.mapper.ProductShowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 商品信息Service业务层处理
 *
 *
 * @author zcc
 */
@Service
@Slf4j
public class ProductShowService extends ServiceImpl<ProductShowMapper, ProductShow> implements IService<ProductShow> {

    @Autowired
    private ProductCategoryMapper productCategoryMapper; // 注入权限验证服务


    public List<ProductShowVO> selectByEntity(ProductShowVO productShow, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
       LoginUser loginUser= SecurityUtils.getLoginUser();
       //审核页list
       if(loginUser.getPermissions().contains("pms:review:list")||loginUser.getPermissions().contains("*:*:*")){
           return baseMapper.selectProductShowList(productShow);
       }
        //申请页list
        productShow.setTenantId(loginUser.getNowTenantId());
        productShow.setStoreId(loginUser.getDeptId());
        return baseMapper.selectProductShowList(productShow);
    }

    //申请商品展示
    public Boolean insertProductShow(ProductShow productShow) {
        LoginUser loginUser= SecurityUtils.getLoginUser();
        productShow.setTenantId(loginUser.getNowTenantId());
        productShow.setStoreId(loginUser.getDeptId());
        productShow.setApplyStatus("0");
        productShow.setApplyTime(LocalDateTime.now());
        return this.save(productShow);
    }
    //获取商品展示详情
    public ProductShow selectProductShowDetail(Long id) {
        return this.getById(id);
    }
    //修改申请(租户)
    public Boolean updateProductShow(ProductShow productShowUpdate) {
        ProductShow dbProductShow =this.getById(productShowUpdate.getId());
        if ("1,3".contains(dbProductShow.getApplyStatus())){
            throw new RuntimeException("审核通过或正在展示期的商品不允许修改");
        }
        if (dbProductShow.getApplyStatus().equals("4")){
            throw new RuntimeException("本次申请展示期已结束，如需继续展示请重新提交申请");
        }
        ProductShow productShow = new ProductShow();
        productShow.setId(productShowUpdate.getId());
        productShow.setProductId(productShowUpdate.getProductId());
        productShow.setShowStartTime(productShowUpdate.getShowStartTime());
        productShow.setShowEndTime(productShowUpdate.getShowEndTime());
        productShow.setApplyStatus("0");
        return this.updateById(productShow);
    }

    //取消申请
    public Boolean deleteProductShow(Long id) {
        return this.removeById(id);
    }



    //拒绝商品展示
    public Boolean refuseProductShow(Long id) {
        ProductShow dbProductShow =this.getById(id);
        if (!dbProductShow.getApplyStatus().equals("0")){
            throw new RuntimeException("当前状态不允许此项操作");
        }
        ProductShow productShow = new ProductShow();
        productShow.setId(id);
        productShow.setApplyStatus("2");
        return this.updateById(productShow);
    }
    //审核通过
    public Boolean passProductShow(ProductShow productShowUpdate) {
        ProductShow dbProductShow =this.getById(productShowUpdate.getId());
        if (ObjectUtil.isEmpty(dbProductShow)){
            throw new RuntimeException("租户已取消申请");
        }
        if (!"0,1,2".contains(dbProductShow.getApplyStatus())){
            throw new RuntimeException("当前状态不允许此项操作");
        }
        if (dbProductShow.getShowStartTime().before(new Date())){
            throw new RuntimeException("展示开始日期已过，请联系租户修改展示时间后，重新提交申请");
        }
        if (ObjectUtil.hasNull(productShowUpdate.getSort(),productShowUpdate.getShowCategory())){
            throw new RuntimeException("请将上架商品所属分类及排序补充完整");
        }
        ProductCategory category=productCategoryMapper.selectById(productShowUpdate.getShowCategory());
        if (ObjectUtil.isNull(category)){
            throw new RuntimeException("上架商品所属分类不存在");
        }
        ProductShow productShow = new ProductShow();
        productShow.setId(productShowUpdate.getId());
        productShow.setReviewTime(productShowUpdate.getReviewTime());
        productShow.setApplyStatus("1");
        productShow.setSort(productShowUpdate.getSort());
        productShow.setShowCategory(productShowUpdate.getShowCategory());
        if (!dbProductShow.getApplyStatus().equals(productShowUpdate.getApplyStatus())){
            productShow.setReviewTime(LocalDateTime.now());
        }
        return this.updateById(productShow);
    }


    //上架商品展示 (定时器触发事件)
    @Transactional
    public int onSaleProductShow() {
        int updatedRows=baseMapper.onSaleProductShow();
        System.out.println("今天开始的展示任务已启动，影响记录数：" + updatedRows);
        return updatedRows;
    }
    //商品展示到期 (定时器触发事件)
    @Transactional
    public int expireProductShow() {
        int updatedRows=baseMapper.expireProductShow();
        System.out.println("昨天结束的展示任务已停止，影响记录数：" + updatedRows);
        return updatedRows;
    }

}
