package com.xhpc.user.service.impl;

import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.security.service.TokenService;
import com.xhpc.system.api.model.LoginUser;
import com.xhpc.user.domain.XhpcDonationInvoice;
import com.xhpc.user.mapper.XhpcDonationInvoiceMapper;
import com.xhpc.user.service.IXhpcDonationInvoiceService;
import org.springframework.stereotype.Service;

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

/**
 * @author yuyang
 * @date 2022/6/22 11:47
 */
@Service
public class XhpcDonationInvoiceImpl implements IXhpcDonationInvoiceService {

    @Resource
    XhpcDonationInvoiceMapper xhpcDonationInvoiceMapper;
    @Resource
    TokenService tokenService;
    /**
     * 添加捐款发票企业
     *
     * @param xhpcDonationInvoice
     * @return
     */
    @Override
    public AjaxResult insertDonationInvoice(XhpcDonationInvoice xhpcDonationInvoice) {
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        //名称不能重复\发票抬头不能重复\发票税号不能重复
        if(xhpcDonationInvoice.getDonationInvoiceId() ==null){
            String name = xhpcDonationInvoice.getName();
            if(name !=null){
                int i = xhpcDonationInvoiceMapper.getDonationInvoiceRepeat(null, name, null, null, tenantId);
                if(i>0){
                    return AjaxResult.error(500, "捐款单位名称重复");
                }
            }
            String titleContent = xhpcDonationInvoice.getTitleContent();
            if(titleContent !=null){
                int i = xhpcDonationInvoiceMapper.getDonationInvoiceRepeat(null, null, titleContent, null, tenantId);
                if(i>0){
                    return AjaxResult.error(500, "发票抬头重复");
                }
            }
            String dutyNumber = xhpcDonationInvoice.getDutyNumber();
            if(dutyNumber !=null){
                int i = xhpcDonationInvoiceMapper.getDonationInvoiceRepeat(null, null, null, dutyNumber, tenantId);
                if(i>0){
                    return AjaxResult.error(500, "发票税号重复");
                }
            }
            xhpcDonationInvoiceMapper.insertDonationInvoice(xhpcDonationInvoice);
            return AjaxResult.success();
        }else{
            Long donationInvoiceId = xhpcDonationInvoice.getDonationInvoiceId();
            String name = xhpcDonationInvoice.getName();
            if(name !=null){
                int i = xhpcDonationInvoiceMapper.getDonationInvoiceRepeat(donationInvoiceId, name, null, null, tenantId);
                if(i>0){
                    return AjaxResult.error(500, "捐款单位名称重复");
                }
            }
            String titleContent = xhpcDonationInvoice.getTitleContent();
            if(titleContent !=null){
                int i = xhpcDonationInvoiceMapper.getDonationInvoiceRepeat(donationInvoiceId, null, titleContent, null, tenantId);
                if(i>0){
                    return AjaxResult.error(500, "发票抬头重复");
                }
            }
            String dutyNumber = xhpcDonationInvoice.getDutyNumber();
            if(dutyNumber !=null){
                int i = xhpcDonationInvoiceMapper.getDonationInvoiceRepeat(donationInvoiceId, null, null, dutyNumber, tenantId);
                if(i>0){
                    return AjaxResult.error(500, "发票税号重复");
                }
            }
            xhpcDonationInvoiceMapper.updateDonationInvoice(xhpcDonationInvoice);
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult getDonationInvoiceById(Long  donationInvoiceId) {
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        return AjaxResult.success(xhpcDonationInvoiceMapper.getDonationInvoiceById(donationInvoiceId,tenantId));
    }
    @Override
    public AjaxResult deleteById(String  donationInvoiceId) {
        LoginUser loginUser = tokenService.getLoginUser();
        String tenantId = loginUser.getTenantId();
        return AjaxResult.success(xhpcDonationInvoiceMapper.updateDonationInvoiceById(Long.parseLong(donationInvoiceId),tenantId));
    }


    @Override
    public List<XhpcDonationInvoice> getPage(Map<String, Object> params) {
        return xhpcDonationInvoiceMapper.getDonationInvoiceMessage(params);
    }
}
