package com.cool.store.dao;

import com.cool.store.dto.partner.PartnerOpenNewShopPageDTO;
import com.cool.store.entity.OpenNewShopRecordDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.OpenNewShopRecordMapper;
import com.cool.store.request.PartnerOpenNewShopPageRequest;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

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

/**
 * @author zhangchenbiao
 * @FileName: OpenNewShopRecordDAO
 * @Description:
 * @date 2024-05-09 15:09
 */
@Repository
public class OpenNewShopRecordDAO {

    @Resource
    private OpenNewShopRecordMapper openNewShopRecordMapper;

    /**
     * 获取开新店数据
     * @param request
     * @return
     */
    public Page<PartnerOpenNewShopPageDTO> getPartnerOpenNewShopPage(PartnerOpenNewShopPageRequest request){
        PageHelper.startPage(request.getPageNum(),request.getPageSize());
        return openNewShopRecordMapper.getPartnerOpenNewShopPage(request);
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    public OpenNewShopRecordDO getOpenNewShopRecordById(Long id){
        return openNewShopRecordMapper.selectByPrimaryKey(id);
    }

    public Integer updateOpenNewShopRecord(OpenNewShopRecordDO openNewShopRecordDO){
        return openNewShopRecordMapper.updateByPrimaryKeySelective(openNewShopRecordDO);
    }

    /**
     * 获取待审核的开新店记录
     * @param lineId
     * @return
     */
    public OpenNewShopRecordDO getWaitAuditOpenNewShopRecord(Long lineId){
        if(Objects.isNull(lineId)){
            return null;
        }
        return openNewShopRecordMapper.getWaitAuditOpenNewShopRecord(lineId);
    }

    public Long insertOpenNewShopRecord(OpenNewShopRecordDO openNewShopRecordDO){
        if(Objects.isNull(openNewShopRecordDO.getLineId())){
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR);
        }
        openNewShopRecordMapper.insertSelective(openNewShopRecordDO);
        return openNewShopRecordDO.getId();
    }

    /**
     * 根据lineId获取开新店记录
     * @param lineId
     * @return
     */
    public List<OpenNewShopRecordDO> getOpenNewShopRecordList(Long lineId){
        if(Objects.isNull(lineId)){
            return Lists.newArrayList();
        }
        return openNewShopRecordMapper.getOpenNewShopRecordList(lineId);
    }

    /**
     * 删除开新店记录
     * @param deleteIds
     */
    public void deleteOpenNewShopRecord(List<Long> deleteIds) {
        if(CollectionUtils.isEmpty(deleteIds)){
            return;
        }
        openNewShopRecordMapper.deleteOpenNewShopRecord(deleteIds);
    }
}
