/*
 * 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.depotAccessRecord.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.depotAccessRecord.param.*;
import vip.xiaonuo.biz.modular.depotGrain.service.DepotGrainService;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.depotAccessRecord.entity.DepotAccessRecord;
import vip.xiaonuo.biz.modular.depotAccessRecord.mapper.DepotAccessRecordMapper;

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

/**
 * 进出场记录表Service接口实现类
 *
 * @author xiang
 * @date 2023/10/23 16:28
 **/
@Service
public class DepotAccessRecordService extends ServiceImpl<DepotAccessRecordMapper, DepotAccessRecord> {

    @Resource
    DepotGrainService depotGrainService;

    public Page<DepotAccessRecord> page(DepotAccessRecordPageParam param) {
        QueryWrapper<DepotAccessRecord> wrapper = new QueryWrapper<>();
        List<String> depotIdLst = depotGrainService.lstOrgGrep();
        wrapper.lambda().in(CollUtil.isNotEmpty(depotIdLst), DepotAccessRecord::getDepotId, depotIdLst);
        if (ObjectUtil.isNotEmpty(param.getDepotId())) {
            wrapper.lambda().eq(DepotAccessRecord::getDepotId, param.getDepotId());
        }
        if (ObjectUtil.isNotEmpty(param.getLicensePlate())) {
            wrapper.lambda().like(DepotAccessRecord::getLicensePlate, param.getLicensePlate());
        }
        if (ObjectUtil.isNotEmpty(param.getLicensePlateColor())) {
            wrapper.lambda().eq(DepotAccessRecord::getLicensePlateColor, param.getLicensePlateColor());
        }
        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(DepotAccessRecord::getId);
        }
        Page page = this.page(CommonPageRequest.defaultPage(), wrapper);
        //匹配粮库名称
        depotGrainService.putDepotName(page.getRecords());
        return page;
    }

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

    public DepotAccessRecord getOneByBookId(String bookId) {
        List<DepotAccessRecord> list = lst(new DepotAccessRecord(bookId));
        return list.isEmpty() ? null : list.get(0);
    }

    public void add(DepotAccessRecordAddPushParam param) {
        DepotAccessRecord depotAccessRecord = this.getOneByBookId(param.getTruckBookId());
        if (depotAccessRecord != null) {
            BeanUtil.copyProperties(param, depotAccessRecord);
            updateById(depotAccessRecord);
            return;
        }
        depotAccessRecord = BeanUtil.toBean(param, DepotAccessRecord.class);
        this.save(depotAccessRecord);
    }

    public void add(DepotAccessRecordAddParam depotAccessRecordAddParam) {
        DepotAccessRecord depotAccessRecord = BeanUtil.toBean(depotAccessRecordAddParam, DepotAccessRecord.class);
        this.save(depotAccessRecord);
    }

    public void editByBookId(DepotAccessRecordAddPushParam param) {
        DepotAccessRecord depotAccessRecord = this.getOneByBookId(param.getTruckBookId());
        param.setId(depotAccessRecord.getId());
        BeanUtil.copyProperties(param, depotAccessRecord);
        this.updateById(depotAccessRecord);
    }

    public void edit(DepotAccessRecordEditParam depotAccessRecordEditParam) {
        DepotAccessRecord depotAccessRecord = this.queryEntity(depotAccessRecordEditParam.getId());
        BeanUtil.copyProperties(depotAccessRecordEditParam, depotAccessRecord);
        this.updateById(depotAccessRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    public void delByBookId(String bookId) {
        this.remove(new LambdaQueryWrapper<>(new DepotAccessRecord(bookId)));
    }

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

    public DepotAccessRecord detail(DepotAccessRecord param) {
        DepotAccessRecord one = null;
        if (Strings.isNotBlank(param.getId()))
            one = this.queryEntity(param.getId());
        else if (Strings.isNotBlank(param.getTruckBookId()))
            one = this.getOneByBookId(param.getTruckBookId());
        if (one != null)
            one.setDepotGrainName(depotGrainService.getNameByDepotId(one.getDepotId()));
        return one;
    }

    public DepotAccessRecord detail(DepotAccessRecordIdParam depotAccessRecordIdParam) {
        return this.queryEntity(depotAccessRecordIdParam.getId());
    }

    public DepotAccessRecord queryEntity(String id) {
        DepotAccessRecord depotAccessRecord = this.getById(id);
        if (ObjectUtil.isEmpty(depotAccessRecord)) {
            throw new CommonException("进出场记录表不存在，id值为：{}", id);
        }
        return depotAccessRecord;
    }

}
