package com.fjhx.service.form.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.base.Condition;
import com.fjhx.entity.form.FormOverflowLoss;
import com.fjhx.mapper.form.FormOverflowLossMapper;
import com.fjhx.service.form.FormOverflowLossService;
import com.fjhx.uitl.code.CodeEnum;
import com.fjhx.utils.UserClientUtil;
import com.fjhx.utils.wrapperUtil.IWrapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-12-27
 */
@Service
public class FormOverflowLossServiceImpl extends ServiceImpl<FormOverflowLossMapper, FormOverflowLoss> implements FormOverflowLossService {

    @Override
    public Page<Map<String, Object>> getPage(Condition condition) {

        IWrapper<Object> wrapper = IWrapper.getWrapper(condition)
                .eq("fol", FormOverflowLoss::getType)
                .like("fol", FormOverflowLoss::getCode)
                .eq("fol", FormOverflowLoss::getWarehouseId)
                .eq("fol", FormOverflowLoss::getCreateUser)
                .periodTime("fol", FormOverflowLoss::getCreateTime)
                .orderByDesc("fol", FormOverflowLoss::getId);

        Page<Map<String, Object>> page = baseMapper.getPage(condition.getPage(), wrapper);

        UserClientUtil.setUserName(page.getRecords(), "createUser", "createUserName");

        return page;
    }

    @Override
    public void addOverflow(Integer businessType, Long businessId, Long warehouseId, BigDecimal amount, String remarks) {

        FormOverflowLoss formOverflowLoss = new FormOverflowLoss();
        formOverflowLoss.setType(1);
        formOverflowLoss.setCode(CodeEnum.FORM_OVERFLOW.getCode());
        formOverflowLoss.setWarehouseId(warehouseId);
        formOverflowLoss.setAmount(amount);
        formOverflowLoss.setRemarks(remarks);
        formOverflowLoss.setBusinessType(businessType);
        formOverflowLoss.setBusinessId(businessId);

        save(formOverflowLoss);
    }

    @Override
    public void addLoss(Integer businessType, Long businessId, Long warehouseId, BigDecimal amount, String remarks) {

        FormOverflowLoss formOverflowLoss = new FormOverflowLoss();
        formOverflowLoss.setType(2);
        formOverflowLoss.setCode(CodeEnum.FORM_LOSS.getCode());
        formOverflowLoss.setWarehouseId(warehouseId);
        formOverflowLoss.setAmount(amount);
        formOverflowLoss.setRemarks(remarks);
        formOverflowLoss.setBusinessType(businessType);
        formOverflowLoss.setBusinessId(businessId);

        save(formOverflowLoss);
    }

}
