package cn.superdesk.uniorder.dubbo.provider.operationCenter.customer;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCustomerInvoiceTitle;
import cn.superdesk.uniorder.dao.entity.operationCenter.UnifiedCenterCustomer;
import cn.superdesk.uniorder.dto.param.operationCenter.base.OperationCenterBaseParam;
import cn.superdesk.uniorder.dto.param.operationCenter.customer.*;
import cn.superdesk.uniorder.dto.vo.operationCenter.customer.OperationCenterBillStartVo;
import cn.superdesk.uniorder.dubbo.facade.operationCenter.customer.DubboOperationCenterBillStartService;
import cn.superdesk.uniorder.service.operationCenter.OperationCenterBillStartService;
import cn.superdesk.uniorder.service.operationCenter.OperationCenterCustomerService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @Author : zx
 * @Create : 2023/4/11 09:49
 * Description : 调操作mapper的service层
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboOperationCenterBillStartServiceImpl implements DubboOperationCenterBillStartService {
    @Autowired
    private OperationCenterBillStartService operationCenterBillStartService;
    @Autowired
    private OperationCenterCustomerService operationCenterCustomerService;

    @Override
    public R<List<OperationCenterBillStartVo>> getBillStartListByCustomerId(OperationCenterBaseParam param) {
        PageHelper.startPage(param.getPageNumber(), param.getPageSize());
        if (null == param.getId()) {
            return new R<>("500","客户id不能为空！",false);
        }
        List<OperationCenterBillStartVo> resultList = operationCenterBillStartService.getBillStartListByCustomerId(param);
        PageInfo<OperationCenterBillStartVo> page       = new PageInfo<OperationCenterBillStartVo>(resultList);
        PageHelper.clearPage();
        return new R<>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(),resultList );
    }

    @Override
    public R<String> saveOperationCenterBillStart(OperationCenterBillStartParam param, Long sysUserId) {
        UnifiedCustomerInvoiceTitle billStart = new UnifiedCustomerInvoiceTitle();

        //根据id区分新增还是修改
        if (null == param.getId()) { //新增
            BeanUtils.copyProperties(param, billStart);
            billStart.setCustomerId(param.getCustomerId());
            billStart.setCreateTime(new Date());
            billStart.setCreateUserId(sysUserId);
            billStart.setUpdateTime(new Date());
            billStart.setUpdateUserId(sysUserId);
            operationCenterBillStartService.insertSelective(billStart);

            //修改客户--开票抬头配置 ：已配置
            UnifiedCenterCustomer customer = new UnifiedCenterCustomer();
            customer.setId(param.getCustomerId());
            customer.setIsBillStart((byte) 1);
            operationCenterCustomerService.updateByPrimaryKeySelective(customer);
        //修改
        }else {
            billStart = operationCenterBillStartService.getUnifiedBillStartById(param.getId());
            BeanUtils.copyProperties(param, billStart);

            billStart.setUpdateTime(new Date());
            billStart.setUpdateUserId(sysUserId);
            operationCenterBillStartService.updateByPrimaryKeySelective(billStart);
        }

        return new R<>("保存成功！");
    }

    @Override
    public R<OperationCenterBillStartVo> getBillStartById(Long id) {
        UnifiedCustomerInvoiceTitle billStart = operationCenterBillStartService.findById(id);
        OperationCenterBillStartVo vo = new OperationCenterBillStartVo();
        BeanUtils.copyProperties(billStart, vo);
        return new R<>(vo);
    }

    @Override
    public R<String> deleteOperationCenterBillStartById(Long id, Long sysUserId) {

        UnifiedCustomerInvoiceTitle entity = operationCenterBillStartService.getUnifiedBillStartById(id);
        if (entity == null) {
            return new R<>("500", "未查询到id：" + id + "开票抬头", false);
        }
        //若该开票抬头为默认不允许删除
        if (1 == entity.getIsDefault()) {
            return new R<>("500", "不允许删除已设为默认的开票抬头，请先取消默认", false);
        }

        operationCenterBillStartService.deleteObj(id);
        log.info("软删除客户开票抬头信息" + DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + "  操作人的用户ID为：" + sysUserId);
        //该客户所关联的“未删除”开票抬头数量=0时，即为“未配置”； ≥1时，即为“已配置”
        int count = operationCenterBillStartService.countBillStartByCustomerId(entity.getCustomerId());
        if (0 == count) {
            UnifiedCenterCustomer customer = new UnifiedCenterCustomer();
            customer.setId(entity.getCustomerId());
            customer.setIsBillStart((byte) 0);
            operationCenterCustomerService.updateByPrimaryKeySelective(customer);
        }


        return new R<>("删除成功！");
    }

    @Override
    public R<String> enableOperationCenterBillStartById(Long billId, Long sysUserId) {
        //首先判断该开票抬头是否存在
        UnifiedCustomerInvoiceTitle billStart = operationCenterBillStartService.getUnifiedBillStartById(billId);
        if (billStart == null) {
            return new R<>("500", "该开票抬头不存在或已删除，请检查", false);
        }
        //判断 是 设为默认 还是取消默认
        //设为默认
        if (0 == billStart.getIsDefault()) {
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("userId", sysUserId);
            conditions.put("isDefault", 0);
            conditions.put("customerId", billStart.getCustomerId());
            //修改 未删除的 并且状态为 默认的，开票抬头 -该客户下的开票抬头-客户id
            operationCenterBillStartService.updateBillStartIsDefault(conditions);
            //再修改 该开票抬头为默认
            conditions.put("isDefault", 1);
            conditions.put("id", billId);
            operationCenterBillStartService.updateBillStartIsDefault(conditions);
            return new R<>("设为默认成功！");
            //取消默认
        }else {
            Map<String, Object> conditions = new HashMap<>();
            conditions.put("userId", sysUserId);
            conditions.put("isDefault", 0);
            conditions.put("id", billId);
            conditions.put("customerId", billStart.getCustomerId());
            operationCenterBillStartService.updateBillStartIsDefault(conditions);
            return new R<>("取消默认成功！");
        }



    }
}
