/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.depotTruckAdmitFlow.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.xiaonuo.biz.modular.depotGrain.service.DepotGrainService;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.entity.DepotTruckAdmitFlow;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.mapper.DepotTruckAdmitFlowMapper;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.param.DepotTruckAdmitFlowAddParam;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.param.DepotTruckAdmitFlowEditParam;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.param.DepotTruckAdmitFlowIdParam;
import vip.xiaonuo.biz.modular.depotTruckAdmitFlow.param.DepotTruckAdmitFlowPageParam;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;

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

/**
 * 车辆接待流程表Service接口
 *
 * @author xiang
 * @date 2023/09/13 18:44
 **/
@Service
public class DepotTruckAdmitFlowService extends ServiceImpl<DepotTruckAdmitFlowMapper, DepotTruckAdmitFlow> {

    @Resource
    DepotGrainService depotGrainService;

    public Page<DepotTruckAdmitFlow> page(DepotTruckAdmitFlowPageParam param) {
        QueryWrapper<DepotTruckAdmitFlow> wrapper = new QueryWrapper<>();
        List<String> depotIdLst = depotGrainService.lstOrgGrep();
        wrapper.lambda().in(CollUtil.isNotEmpty(depotIdLst), DepotTruckAdmitFlow::getDepotId, depotIdLst);
        wrapper.lambda().eq(ObjectUtil.isNotEmpty(param.getDepotId()), DepotTruckAdmitFlow::getDepotId, param.getDepotId());
        wrapper.lambda().like(Strings.isNotBlank(param.getTruckBookId()), DepotTruckAdmitFlow::getTruckBookId, param.getTruckBookId());
        wrapper.lambda().eq(Strings.isNotBlank(param.getDepotBizType()), DepotTruckAdmitFlow::getDepotBizType, param.getDepotBizType());
        wrapper.lambda().like(ObjectUtil.isNotEmpty(param.getLicensePlate()), DepotTruckAdmitFlow::getLicensePlate, param.getLicensePlate());
        if (ObjectUtil.isAllNotEmpty(param.getSortField(), param.getSortOrder())) {
            CommonSortOrderEnum.validate(param.getSortOrder());
            wrapper.orderBy(true, param.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()),
                    StrUtil.toUnderlineCase(param.getSortField()));
        } else {
            wrapper.lambda().orderByDesc(DepotTruckAdmitFlow::getId);
        }
        Page page = this.page(CommonPageRequest.defaultPage(), wrapper);
        //匹配粮库名称
        depotGrainService.putDepotName(page.getRecords());
        return page;
    }

    public List<DepotTruckAdmitFlow> lst(DepotTruckAdmitFlow param) {
        return list(new LambdaQueryWrapper<>(param));
    }

    public void add(DepotTruckAdmitFlowAddParam param) {
        this.save(BeanUtil.toBean(param, DepotTruckAdmitFlow.class));
    }

    public void edit(DepotTruckAdmitFlowEditParam depotTruckAdmitFlowEditParam) {
        DepotTruckAdmitFlow depotTruckAdmitFlow = this.queryEntity(depotTruckAdmitFlowEditParam.getId());
        BeanUtil.copyProperties(depotTruckAdmitFlowEditParam, depotTruckAdmitFlow);
        this.updateById(depotTruckAdmitFlow);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(List<DepotTruckAdmitFlowIdParam> depotTruckAdmitFlowIdParamList) {
        // 执行删除
        this.removeBatchByIds(CollStreamUtil.toList(depotTruckAdmitFlowIdParamList, DepotTruckAdmitFlowIdParam::getId));
    }

    public DepotTruckAdmitFlow detail(DepotTruckAdmitFlowIdParam depotTruckAdmitFlowIdParam) {
        return this.queryEntity(depotTruckAdmitFlowIdParam.getId());
    }

    public DepotTruckAdmitFlow queryEntity(String id) {
        DepotTruckAdmitFlow depotTruckAdmitFlow = this.getById(id);
        if (ObjectUtil.isEmpty(depotTruckAdmitFlow)) {
            throw new CommonException("车辆接待流程表不存在，id值为：{}", id);
        }
        return depotTruckAdmitFlow;
    }
}
