package com.cool.store.dao;

import com.cool.store.dto.pre.fry.ApplyDetailDTO;
import com.cool.store.dto.pre.fry.ApplyManagementDTO;
import com.cool.store.dto.pre.fry.ApplyManagementQueryDTO;
import com.cool.store.entity.PreFryQualificationApplyDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.PreFryQualificationApplyMapper;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @Author suzhuhong
 * @Date 2025/6/20 19:12
 * @Version 1.0
 */
@Repository
public class PreFryQualificationApplyDAO {

    @Resource
    PreFryQualificationApplyMapper preFryQualificationApplyMapper;


    /**
     * 根据ID查询资质申请
     * @param id
     * @return
     */
    public PreFryQualificationApplyDO queryById(Long id) {
        //校验ID不为空
        if (id == null){
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        return preFryQualificationApplyMapper.selectByPrimaryKey(id);
    }


    /**
     * 新增资质申请
     * @param preFryQualificationApplyDO
     * @return
     */
    public int createQualificationApply(PreFryQualificationApplyDO preFryQualificationApplyDO) {
        return preFryQualificationApplyMapper.insertSelective(preFryQualificationApplyDO);
    }

    /**
     * 强制修改资质申请 非空置为空
     * @param preFryQualificationApplyDO
     * @return
     */
    public int updateForce(PreFryQualificationApplyDO preFryQualificationApplyDO) {
        return preFryQualificationApplyMapper.updateForce(preFryQualificationApplyDO);
    }


    /**
     * 根据门店编码查询申请记录列表
     * @param storeCode 门店编码
     * @return 申请记录列表
     */
    public List<PreFryQualificationApplyDO> listByStoreCode(String storeCode) {
        // 参数校验
        if (StringUtils.isBlank(storeCode)) {
           return Lists.newArrayList();
        }
        return preFryQualificationApplyMapper.listByStoreCode(storeCode);
    }

    public PreFryQualificationApplyDO selectByStoreCodeAndType(String storeCode, Integer applyType) {
        // 参数校验
        if (StringUtils.isBlank(storeCode)|| applyType==null) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        return preFryQualificationApplyMapper.selectByStoreCodeAndType(storeCode,applyType);
    }

    /**
     * 查询指定门店applyType最小的申请记录
     * @param storeCode 门店编码
     * @return 申请记录
     */
    public PreFryQualificationApplyDO getMinApplyTypeByStoreCode(String storeCode) {
        // 参数校验
        if (StringUtils.isBlank(storeCode)) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED, "门店编码不能为空");
        }
        return preFryQualificationApplyMapper.selectMinApplyTypeByStoreCode(storeCode);
    }

    public List<ApplyManagementDTO> selectApplyManagementList(ApplyManagementQueryDTO query) {
        return preFryQualificationApplyMapper.selectApplyManagementList(query);
    }

    public ApplyDetailDTO getApplyDetail(Long id) {
        if (id==null) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_REQUIRED);
        }
        return preFryQualificationApplyMapper.getDetail(id);
    }

}
