package com.jwipc.sys.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.exception.DaoException;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.convert.TaxConvert;

import com.jwipc.sys.dto.*;
import com.jwipc.sys.entity.MoldForm;
import com.jwipc.sys.entity.TaxInfo;

import com.jwipc.sys.logic.TaxLogic;
import com.jwipc.sys.model.param.TaxParam;
import com.jwipc.sys.model.po.TaxPO;

import com.jwipc.sys.model.query.TaxQuery;

import com.jwipc.sys.service.ITaxService;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 采购组主数据 服务实现类
 * </p>
 *
 * @author AL
 * @since 2024-01-29
 */
@Service
@RequiredArgsConstructor
public class TaxLogicImpl implements TaxLogic {
    private final ITaxService taxService;
    private final TaxConvert taxConvert;

    @Override
    public PageView findByPage(TaxQuery query) {
        final Page<TaxInfo> dbPage = PageUtil.toPage(query);

        List<TaxPO> taxPOS = taxService.findPageByQuery(dbPage, query);

        return PageUtil.getPV(taxConvert.po2PageListDTO1(taxPOS), dbPage.getTotal());
    }

    @Override
    public List<TaxDTO> listAll(TaxQuery query) {
        List<TaxPO> taxPOS = taxService.findPageByQuery(null, query);
        return taxConvert.po2PageListDTO1(taxPOS);
    }

    @Override
    public TaxDTO getById(Long id) {
        if (id != null) {
            TaxPO taxPO = taxService.getDataById(id);
            return taxConvert.po2DTO(taxPO);
        } else {
            return new TaxDTO();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(TaxParam param) {
        //保存
//        Objects.nonNull();
//        Objects.isNull()
        if (Objects.isNull(param.getId())) {
            final LambdaQueryWrapper<TaxInfo> resourcesLambdaQueryWrapper =
                    new LambdaQueryWrapper<TaxInfo>().eq(
                            TaxInfo::getTaxCode, param.getTaxCode()).last(" limit 1");
            if (ObjectUtil.isNotNull(taxService.getOne(resourcesLambdaQueryWrapper))) {
                throw new DaoException("税率编码重复");
            }

            param.setCreateTime(LocalDateTime.now());
            param.setCreateUser(LoginUtil.getUserId());
            param.setCreateUserName(LoginUtil.getName());


        } else {
            final LambdaQueryWrapper<TaxInfo> resourcesLambdaQueryWrapper =
                    new LambdaQueryWrapper<TaxInfo>().eq(
                                    TaxInfo::getTaxCode, param.getTaxCode())
                            .ne(TaxInfo::getId, param.getId()).last(" limit 1");
            if (ObjectUtil.isNotNull(taxService.getOne(resourcesLambdaQueryWrapper))) {
                throw new DaoException("税率编码重复");
            }


        }
        taxService.saveOrUpdate(taxConvert.param2Entity(param));
    }

    @Override
    public void remove(String taxCode) {
        //删除
        taxService.remove(
                new LambdaQueryWrapper<TaxInfo>().eq(TaxInfo::getTaxCode, taxCode));
    }
}
