package com.chushang.inspection.terminal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chushang.common.mybatis.enums.Operator;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.common.mybatis.utils.WrapperUtils;
import com.chushang.datascope.annotation.DataScope;
import com.chushang.inspection.project.po.WrkAudit;
import com.chushang.inspection.terminal.dto.ConsumablesTotalDTO;
import com.chushang.inspection.terminal.po.ConsumableDetails;
import com.chushang.inspection.terminal.query.ConsumablesDetailsQuery;
import com.chushang.inspection.terminal.query.ConsumablesTotalQuery;
import com.chushang.inspection.terminal.service.ConsumableDetailsService;
import com.chushang.inspection.terminal.service.ConsumablesTotalService;
import com.chushang.security.entity.po.SysUser;
import com.chushang.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chushang.inspection.terminal.po.ConsumablesTotal;
import com.chushang.inspection.terminal.mapper.ConsumablesTotalMapper;
import org.springframework.transaction.annotation.Transactional;

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

/**
  * @auther: zhao
  * @date: 2024/6/21 11:47
  */
@Slf4j
@Service
public class ConsumablesTotalServiceImpl extends ServiceImpl<ConsumablesTotalMapper, ConsumablesTotal> implements ConsumablesTotalService {

    @Resource
    ConsumableDetailsService detailsService;

    @Override
    @DataScope
    public PageResult findPage(ConsumablesTotalQuery query) {
        CommonParam commonParam = CommonParam.buildPageRequest();
        LambdaQueryWrapper<ConsumablesTotal> totalSql = WrapperUtils.builder(query, commonParam);
        IPage<ConsumablesTotal> page = this.page(
                new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(commonParam.getPage(), commonParam.getLimit()),
                totalSql
        );
        return new PageResult(page);
    }

    @Override
    @Transactional
    public void storage(ConsumablesTotalDTO total) {
        LambdaQueryWrapper<ConsumablesTotal> totalSql = WrapperUtils.builder();
        totalSql.eq(ConsumablesTotal::getTaskId, total.getTaskId())
                .eq(ConsumablesTotal::getType, total.getType())
                .last(Operator.LIMIT_ONE.getCharacter());
        ConsumablesTotal consumablesTotal = getOne(totalSql);
        if (consumablesTotal == null) {
            consumablesTotal = BeanUtil.copyProperties(total, ConsumablesTotal.class);
            consumablesTotal.setDeptId(SecurityUtils.getDeptId());
            save(consumablesTotal);
        } else {
            consumablesTotal.setTotal(consumablesTotal.getTotal() + total.getTotal());
            updateById(consumablesTotal);
        }

        detailsService.saveDetails(consumablesTotal, total.getTotal(), 1);
    }

    @Override
    public void outOfWarehouse(ConsumablesTotalDTO total) {
        ConsumablesTotal consumablesTotal = getById(total.getTotalId());
        Assert.isTrue(consumablesTotal == null || consumablesTotal.getTotal() < total.getTotal()
                , "耗材不存在或耗材总数不够出库的数量");
        assert consumablesTotal != null;
        consumablesTotal.setTotal(consumablesTotal.getTotal() - total.getTotal());
        updateById(consumablesTotal);
        // 出库
        detailsService.saveDetails(consumablesTotal, total.getTotal(), 2);
    }
}
