package cn.iocoder.yudao.module.basic.service.goodsverify;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.basic.api.redis.RedisBasicInfoUtilApi;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyCreateReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyPageReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyRespVO;
import cn.iocoder.yudao.module.basic.controller.admin.goodsverify.vo.GoodsVerifyUpdateReqVO;
import cn.iocoder.yudao.module.basic.convert.goodsverify.GoodsVerifyConvert;
import cn.iocoder.yudao.module.basic.dal.dataobject.goodsverify.GoodsVerifyDO;
import cn.iocoder.yudao.module.basic.dal.mysql.goodsverify.GoodsVerifyMapper;
import cn.iocoder.yudao.module.basic.service.businessinfo.BusinessInfoServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.basic.enums.ErrorCodeConstants.*;

/**
 * 商品认证 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class GoodsVerifyServiceImpl implements GoodsVerifyService {

    @Resource
    private GoodsVerifyMapper goodsVerifyMapper;

    @Resource
    private BusinessInfoServiceImpl businessInfoService;

    @Resource
    private RedisBasicInfoUtilApi redisUtil;

    /**
     * 新增商品认证功能
     * @param createReqVO 创建信息
     * @return
     */
    @Override
    public Long createGoodsVerify(GoodsVerifyCreateReqVO createReqVO) {
        //校验所属商户是否存在
        businessInfoService.validateBusinessInfoExists(createReqVO.getBelongBusinessId());

        // 插入
        GoodsVerifyDO goodsVerify = GoodsVerifyConvert.INSTANCE.convert(createReqVO);
        goodsVerifyMapper.insert(goodsVerify);
        // 返回
        return goodsVerify.getId();

    }

    /**
     * 修改商品认证
     * @param updateReqVO 更新信息
     */
    @Override
    public void updateGoodsVerify(GoodsVerifyUpdateReqVO updateReqVO) {
        //校验所属商户是否存在
        businessInfoService.validateBusinessInfoExists(updateReqVO.getBelongBusinessId());

        //跟新记录
        GoodsVerifyDO convert = GoodsVerifyConvert.INSTANCE.convert(updateReqVO);
        convert.setVerifyEndTime(updateReqVO.getVerifyEndTime());
        goodsVerifyMapper.updateById(convert);
    }

    /**
     * 删除商品认证
     *
     * @param id 编号
     */
    @Override
    public void deleteGoodsVerify(Long id) {
        // 校验存在
        validateGoodsVerifyExists(id);
        // 删除
        goodsVerifyMapper.deleteById(id);
    }

    private void validateGoodsVerifyExists(Long id) {
        if (goodsVerifyMapper.selectById(id) == null) {
            throw exception(GOODS_VERIFY_NOT_EXISTS);
        }
    }

    /**
     * 获得商品认证
     *
     * @param id 编号
     * @return 商品认证
     */
    @Override
    public GoodsVerifyDO getGoodsVerify(Long id) {
        return goodsVerifyMapper.selectById(id);
    }

    @Override
    public List<GoodsVerifyDO> getGoodsVerifys(String goodsId, Long belongBusinessId) {
        return goodsVerifyMapper.selectList(new LambdaQueryWrapperX<GoodsVerifyDO>().eq(GoodsVerifyDO::getGoodsId, goodsId)
                .eq(GoodsVerifyDO::getBelongBusinessId, belongBusinessId));
    }

    /**
     * 获得商品认证分页
     *
     * @param pageReqVO 分页查询
     * @return 商品认证分页
     */
    @Override
    public PageResult<GoodsVerifyDO> getGoodsVerifyPage(GoodsVerifyPageReqVO pageReqVO) {
        // 调用MyBatis-Plus提供的分页查询方法，获取分页结果
        PageResult<GoodsVerifyDO> goodsVerifyDOPageResult = new PageResult<>();
        // 获取查询结果中的记录列表
        if (pageReqVO.getBelongBusinessName() != null && !Objects.equals(pageReqVO.getBelongBusinessName(), "")) {
            Page<GoodsVerifyDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
            IPage<GoodsVerifyDO> goodsVerifyDOIPage = goodsVerifyMapper.selectPageByInfo(page, new QueryWrapperX<GoodsVerifyDO>()
                    .likeIfPresent("goods_name", pageReqVO.getGoodsName())
                    .likeIfPresent("verify_name", pageReqVO.getVerifyName())
                    .eqIfPresent("verify_agencies", pageReqVO.getVerifyAgencies())
                    .betweenIfPresent("verify_start_time", pageReqVO.getVerifyStartTime())
                    .betweenIfPresent("verify_end_time", pageReqVO.getVerifyEndTime())
                    .likeIfPresent("bbi.business_name", pageReqVO.getBelongBusinessName())
                    .eq("bgv.deleted",0));

            goodsVerifyDOPageResult.setList(goodsVerifyDOIPage.getRecords());
            goodsVerifyDOPageResult.setTotal(goodsVerifyDOIPage.getTotal());
        } else {
            goodsVerifyDOPageResult = goodsVerifyMapper.selectPage(pageReqVO);
        }
        goodsVerifyDOPageResult.getList().forEach(item -> {
            if (redisUtil.validateBusinessInfoRedisExists(item.getBelongBusinessId())) {
                item.setBelongBusinessName(redisUtil.getBusinessInfoRedis(item.getBelongBusinessId()).getBusinessName());
            }else {
                item.setBelongBusinessName("所属商户不存在");
            }
        });

        // 返回修改后的分页结果
        return goodsVerifyDOPageResult;
    }

    @Override
    public List<GoodsVerifyDO> getByGoodsId(String goodsId) {
        return goodsVerifyMapper.selectList(new LambdaQueryWrapperX<GoodsVerifyDO>().eq(GoodsVerifyDO::getGoodsId, goodsId));
    }

}
