package com.ruixuan.live.service.impl;

import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.utils.Check;
import com.ruixuan.live.entity.KuaishouLiveCustomer;
import com.ruixuan.live.entity.KuaishouLiveCustomerInvoice;
import com.ruixuan.live.mapper.KuaishouLiveCustomerInvoiceMapper;
import com.ruixuan.live.mapper.KuaishouLiveCustomerMapper;
import com.ruixuan.live.service.IKuaishouLiveCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 客户管理Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-01
 */
@Service
public class KuaishouLiveCustomerServiceImpl implements IKuaishouLiveCustomerService {
    @Autowired
    private KuaishouLiveCustomerMapper kuaishouLiveCustomerMapper;
    @Autowired
    private KuaishouLiveCustomerInvoiceMapper kuaishouLiveCustomerInvoiceMapper;

    /**
     * 查询客户管理
     *
     * @param ksId 客户管理主键
     * @return 客户管理
     */
    @Override
    public KuaishouLiveCustomer selectKuaishouLiveCustomerByKsId(Long ksId) {
        return kuaishouLiveCustomerMapper.selectKuaishouLiveCustomerByKsId(ksId);
    }

    /**
     * 查询客户管理列表
     *
     * @param kuaishouLiveCustomer 客户管理
     * @return 客户管理
     */
    @Override
    public List<KuaishouLiveCustomer> selectKuaishouLiveCustomerList(KuaishouLiveCustomer kuaishouLiveCustomer) {
        return kuaishouLiveCustomerMapper.selectKuaishouLiveCustomerList(kuaishouLiveCustomer);
    }

    /**
     * 新增客户管理
     *
     * @param kuaishouLiveCustomer 客户管理
     * @return 结果
     */
    @Override
    public ResultResponse insertKuaishouLiveCustomer(KuaishouLiveCustomer kuaishouLiveCustomer) {
        KuaishouLiveCustomer customer = kuaishouLiveCustomerMapper.selectKuaishouLiveCustomerByKsId(kuaishouLiveCustomer.getKsId());
        if (Check.isNotNull(customer)) {
            return ResultResponse.error("快手小店ID已存在");
        }
        kuaishouLiveCustomerMapper.insertKuaishouLiveCustomer(kuaishouLiveCustomer);
        return ResultResponse.success();
    }

    /**
     * 修改客户管理
     *
     * @param kuaishouLiveCustomer 客户管理
     * @return 结果
     */
    @Override
    public ResultResponse updateKuaishouLiveCustomer(KuaishouLiveCustomer kuaishouLiveCustomer) {
        kuaishouLiveCustomerMapper.updateKuaishouLiveCustomer(kuaishouLiveCustomer);
        return ResultResponse.success();
    }

    @Override
    public ResultResponse addInvoice(KuaishouLiveCustomerInvoice invoice) {
        KuaishouLiveCustomerInvoice entity = kuaishouLiveCustomerInvoiceMapper.getByKsIdAndName(invoice.getKsId(), invoice.getMainBodyName());
        if (Check.isNotNull(entity)) {
            return ResultResponse.error("客户下已存在该资质主体，请重新填写");
        }
        invoice.setId(UUID.randomUUID().toString().replace("-", ""));
        kuaishouLiveCustomerInvoiceMapper.insertKuaishouLiveCustomerInvoice(invoice);
        KuaishouLiveCustomer customer = new KuaishouLiveCustomer();
        customer.setKsId(invoice.getKsId());
        customer.setInvoiceStatus(1);
        kuaishouLiveCustomerMapper.updateKuaishouLiveCustomer(customer);
        return ResultResponse.success();
    }

}
