package com.jianshui.platform.service.impl;

import java.util.List;

import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.core.domain.entity.Companyservice;
import com.jianshui.common.core.domain.entity.SysUser;
import com.jianshui.common.utils.DateUtils;
import com.jianshui.common.utils.SecurityUtils;
import com.jianshui.system.mapper.CompanyserviceMapper;
import com.jianshui.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jianshui.platform.mapper.InvoiceParamConfigMapper;
import com.jianshui.platform.domain.InvoiceParamConfig;
import com.jianshui.platform.service.IInvoiceParamConfigService;

/**
 * 导入设置Service业务层处理
 * 
 * @author kane
 * @date 2023-06-28
 */
@Service
public class InvoiceParamConfigServiceImpl implements IInvoiceParamConfigService 
{
    @Autowired
    private InvoiceParamConfigMapper invoiceParamConfigMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private CompanyserviceMapper companyserviceMapper;

    /**
     * 查询导入设置
     * 
     * @param id 导入设置主键
     * @return 导入设置
     */
    @Override
    public InvoiceParamConfig selectInvoiceParamConfigById(Long id)
    {
        return invoiceParamConfigMapper.selectInvoiceParamConfigById(id);
    }

    /**
     * 查询导入设置列表
     * 
     * @param invoiceParamConfig 导入设置
     * @return 导入设置
     */
    @Override
    public List<InvoiceParamConfig> selectInvoiceParamConfigList(InvoiceParamConfig invoiceParamConfig)
    {
        return invoiceParamConfigMapper.selectInvoiceParamConfigList(invoiceParamConfig);
    }

    /**
     * 新增导入设置
     * 
     * @param invoiceParamConfig 导入设置
     * @return 结果
     */
    @Override
    public int insertInvoiceParamConfig(InvoiceParamConfig invoiceParamConfig)
    {
        //获取企业id
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        Companyservice companyservice = companyserviceMapper.selectCompanyserviceByCompanyid(sysUser.getCompanyId());
        InvoiceParamConfig byCompanyServiceName = invoiceParamConfigMapper.findByCompanyServiceName(companyservice.getSellername());
        if (byCompanyServiceName != null){
            // 删除对应数据
            invoiceParamConfigMapper.deleteInvoiceParamConfigById(byCompanyServiceName.getId());
            // 封装参数
            invoiceParamConfig.setCreateTime(DateUtils.getNowDate());
            invoiceParamConfig.setCompanyName(companyservice.getSellername());
            invoiceParamConfig.setCreateBy(sysUser.getCreateBy());
            //存入对应数据
            return invoiceParamConfigMapper.insertInvoiceParamConfig(invoiceParamConfig);
        }else {
            // 封装参数
            invoiceParamConfig.setCreateTime(DateUtils.getNowDate());
            invoiceParamConfig.setCompanyName(companyservice.getSellername());
            invoiceParamConfig.setCreateBy(sysUser.getCreateBy());
            //存入对应数据
            return invoiceParamConfigMapper.insertInvoiceParamConfig(invoiceParamConfig);
        }



    }

    /**
     * 修改导入设置
     * 
     * @param invoiceParamConfig 导入设置
     * @return 结果
     */
    @Override
    public int updateInvoiceParamConfig(InvoiceParamConfig invoiceParamConfig)
    {
        invoiceParamConfig.setUpdateTime(DateUtils.getNowDate());
        return invoiceParamConfigMapper.updateInvoiceParamConfig(invoiceParamConfig);
    }

    /**
     * 批量删除导入设置
     * 
     * @param ids 需要删除的导入设置主键
     * @return 结果
     */
    @Override
    public int deleteInvoiceParamConfigByIds(Long[] ids)
    {
        return invoiceParamConfigMapper.deleteInvoiceParamConfigByIds(ids);
    }

    /**
     * 删除导入设置信息
     * 
     * @param id 导入设置主键
     * @return 结果
     */
    @Override
    public int deleteInvoiceParamConfigById(Long id)
    {
        return invoiceParamConfigMapper.deleteInvoiceParamConfigById(id);
    }

    /**
     * 功能描述: 获取企业配置
     * @return : com.jianshui.common.core.domain.AjaxResult
     */
    @Override
    public AjaxResult findByName() {
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        Companyservice companyservice = companyserviceMapper.selectCompanyserviceByCompanyid(sysUser.getCompanyId());
        InvoiceParamConfig invoiceParamConfig = invoiceParamConfigMapper.findByCompanyServiceName(companyservice.getSellername());
        return AjaxResult.success(invoiceParamConfig);
    }
}
