package com.zcloud.modules.custormerInfo.service.impl;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.zcloud.common.utils.PageUtils;
 import com.zcloud.datasource.annotation.DataSource;
 import com.zcloud.modules.custormerInfo.dao.BusInvoicingAuditHistoryDao;
 import com.zcloud.modules.custormerInfo.entity.BusInvoicingAuditHistoryEntity;
 import com.zcloud.modules.custormerInfo.service.BusInvoicingAuditHistoryService;
 import com.zcloud.modules.invoicing.entity.BusInvoicingReturnEntity;
 import com.zcloud.modules.invoicing.service.BusInvoicingReturnService;
 import lombok.RequiredArgsConstructor;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

 import java.util.List;
 import java.util.Map;

/**
 * @Author lizenan
 * @Desc 开票表(用于记录原值，每次更新开票表都插入原来的值)(BusInvoicingAuditHistory)表服务实现类
 * @Date 2024-04-03 15:20:56
 */
@Service("busInvoicingAuditHistoryService")
public class BusInvoicingAuditHistoryServiceImpl extends ServiceImpl<BusInvoicingAuditHistoryDao, BusInvoicingAuditHistoryEntity> implements BusInvoicingAuditHistoryService {

    @Autowired
    private BusInvoicingReturnService busInvoicingReturnService;

    @Override
    @DataSource("slave1")
	public BusInvoicingAuditHistoryEntity queryByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusInvoicingAuditHistoryEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectOne(queryWrapper);
	}

	@Override
    @DataSource("slave1")
	public List<BusInvoicingAuditHistoryEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusInvoicingAuditHistoryEntity> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(paramName, paramValue); // 根据传入的参数名和参数值动态构建查询条件

		return baseMapper.selectList(queryWrapper);
	}

    @Override
    @DataSource("slave1")
    public PageUtils listPage(Map<String, Object> params) {
        Page<Map<String,Object>> page = new Page<>(Integer.parseInt(params.get("curPage").toString()),Integer.parseInt(params.get("limit").toString()));
        //申请时间
        if(params.get("applyTimes")!=null){
            List<String> applyTimes = (List<String>) params.get("applyTimes");
            params.put("applyTimeStart",applyTimes.get(0));
            params.put("applyTimeEnd",applyTimes.get(1));
        }
        params.put("isDelete",0);
        IPage<BusInvoicingAuditHistoryEntity> ipage = baseMapper.listPage(page,params);
        return new PageUtils(ipage);
    }


   @Override
	public void delete(String invoicingAuditHistoryId,String userId){
		UpdateWrapper<BusInvoicingAuditHistoryEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("invoicing_audit_history_id",invoicingAuditHistoryId);

		BusInvoicingAuditHistoryEntity busInvoicingAuditHistory = new BusInvoicingAuditHistoryEntity();
		busInvoicingAuditHistory.setOperator(userId);
		busInvoicingAuditHistory.setIsDelete(1);

		this.update(busInvoicingAuditHistory, updateWrapper);
	}

    @Override
    @DataSource("slave1")
    public BusInvoicingAuditHistoryEntity info(String invoicingAuditHistoryId) {
        BusInvoicingAuditHistoryEntity entity = baseMapper.selectById(invoicingAuditHistoryId);
        entity.setInvoicingTypeName(entity.getInvoicingType() == 1?"购买课程":entity.getInvoicingType() == 2?"流量充值":"QPS充值");
        List<BusInvoicingReturnEntity> invoicingReturnList = busInvoicingReturnService.listByInvoicingId(entity.getInvoicingId());
        entity.setInvoicingReturnList(invoicingReturnList);
        return entity;
    }

    @Override
    @DataSource("slave1")
    public List<BusInvoicingAuditHistoryEntity> listByInvoicingId(String invoicingId) {
        return baseMapper.listByInvoicingId(invoicingId);
    }
}
