package com.cf.imes.module.system.service.funds.products;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cf.imes.framework.common.exception.ServiceException;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.Assert.AssertUtils;
import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cf.imes.framework.mybatis.core.query.LambdaUpdateWrapperX;
import com.cf.imes.module.system.controller.admin.funds.products.vo.product.ProductPageReqVO;
import com.cf.imes.module.system.controller.admin.funds.products.vo.product.ProductSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.funds.products.ProductsDO;
import com.cf.imes.module.system.dal.mysql.funds.products.ProductsMapper;
import com.cf.imes.module.system.enums.products.ProductStatusEnum;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;

import java.util.List;

import static com.cf.imes.module.system.enums.ErrorCodeConstants.*;

/**
 * 产品 products Service 实现类
 *
 * @author 晨丰科技
 */
@Service
@Validated
public class ProductsServiceImpl implements ProductsService {

    @Resource
    private ProductsMapper productsMapper;

    @Override
    public Long create(ProductSaveReqVO createReqVO) {
        String productName = createReqVO.getProductName();
        // 校验产品名称唯一
        validateProductNameUnique(null, productName);

        ProductsDO productsDO = new ProductsDO()
                .setProductName(productName)
                .setPrice(createReqVO.getPrice())
                .setStatus(createReqVO.getStatus())
                .setSoftwareId(0);
        productsMapper.insert(productsDO);
        return productsDO.getId();
    }



    @Override
    public void update(ProductSaveReqVO updateReqVO) {
        Long updateReqVOId = updateReqVO.getId();
        String productName = updateReqVO.getProductName();
        // 目标状态
        Integer updateReqVOStatus = updateReqVO.getStatus();
        // 校验产品是否存在
        Integer status = validateProductPlateExists(updateReqVOId).getStatus();
        // 校验产品名称唯一
        validateProductNameUnique(updateReqVOId, productName);

        // 当前状态是上架状态 && 目标状态不是下架 的操作提示已上架
        if (ProductStatusEnum.PRODUCT_UPDATES.getStatus().equals(status) && !ProductStatusEnum.isProductDelis(updateReqVOStatus)) {
            throw new ServiceException(PRODUCTS_NO_UPDATE);
        }
        productsMapper.update(new LambdaUpdateWrapperX<ProductsDO>()
                .eq(ProductsDO::getId, updateReqVOId)
                .setIfPresent(ProductsDO::getProductName, productName)
                .setIfPresent(ProductsDO::getPrice, updateReqVO.getPrice())
                .set(ProductsDO::getStatus, updateReqVOStatus)
        );
    }


    @Override
    public void delete(Long id) {

        ProductsDO productsDO = validateProductPlateExists(id);

        if(productsDO.getStatus().equals(ProductStatusEnum.PRODUCT_UPDATES.getStatus())){
            throw new ServiceException(PRODUCTS_NO_DELETE);
        }

        productsMapper.update(new LambdaUpdateWrapper<ProductsDO>()
                .eq(ProductsDO::getId,id)
                .set(ProductsDO::getDeleted, true));

    }


    @Override
    public ProductsDO get(Long id) {
        return validateProductPlateExists(id);
    }

    @Override
    public PageResult<ProductsDO> getPage(ProductPageReqVO pageReqVO) {
        return productsMapper.selectPage(pageReqVO, false);
    }


    /**
     * 校验产品id是否存在
     *
     * @param id
     * @return
     */
    private ProductsDO validateProductPlateExists(Long id) {
        ProductsDO productsDO = productsMapper.selectOne(new LambdaQueryWrapperX<ProductsDO>()
                .eq(ProductsDO::getId, id)
                .eq(ProductsDO::getDeleted, false));

        if (productsDO == null) {
            throw new ServiceException(PRODUCTS_NO_EXIST);
        }
        return productsDO;
    }

    /**
     * 校验产品名称是否存在
     *
     * @param productName
     */
    private void validateProductNameUnique(Long productId, String productName) {
        List<ProductsDO> productsDOS = productsMapper.selectList(new LambdaQueryWrapperX<ProductsDO>()
                .eq(ProductsDO::getDeleted, false)
                .eq(ProductsDO::getProductName, productName)
                .neIfPresent(ProductsDO::getId, productId)
                .select(ProductsDO::getId));

        AssertUtils.empty(productsDOS, PRODUCTS_IS_EXIST, productName);
    }

}
