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.BusInvoiceManagementDao;
 import com.zcloud.modules.custormerInfo.entity.BusInvoiceManagementEntity;
 import com.zcloud.modules.custormerInfo.service.BusInvoiceManagementService;
 import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import org.springframework.util.ObjectUtils;

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

/**
 * @Author lizenan
 * @Desc 发票抬头管理表(BusInvoiceManagement)表服务实现类
 * @Date 2024-04-03 15:34:45
 */
@Service("busInvoiceManagementService")
public class BusInvoiceManagementServiceImpl extends ServiceImpl<BusInvoiceManagementDao, BusInvoiceManagementEntity> implements BusInvoiceManagementService {

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

		return baseMapper.selectOne(queryWrapper);
	}

	@Override
	@DataSource("slave1")
	public List<BusInvoiceManagementEntity> queryListByFlexibleParams(String paramName, Object paramValue) {
		QueryWrapper<BusInvoiceManagementEntity> 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()));
        params.put("isDelete",0);
        IPage<BusInvoiceManagementEntity> ipage = baseMapper.listPage(page,params);
        return new PageUtils(ipage);
    }

	@Override
	@DataSource("slave1")
	public List<BusInvoiceManagementEntity> listByCustomerinfoId(BusInvoiceManagementEntity param) {
		LambdaQueryWrapper<BusInvoiceManagementEntity> wrapper = new LambdaQueryWrapper<BusInvoiceManagementEntity>();
		wrapper.eq(BusInvoiceManagementEntity::getIsDelete,0);
		wrapper.eq(BusInvoiceManagementEntity::getCustomerinfoId,param.getCustomerinfoId());
		if (!ObjectUtils.isEmpty(param.getInvoiceType())) {
			wrapper.eq(BusInvoiceManagementEntity::getInvoiceType, param.getInvoiceType());
		}
		return baseMapper.selectList(wrapper);
	}


   @Override
	public void delete(String invoiceId,String userId){
		UpdateWrapper<BusInvoiceManagementEntity> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("invoice_id",invoiceId);

		BusInvoiceManagementEntity busInvoiceManagement = new BusInvoiceManagementEntity();
		busInvoiceManagement.setOperator(userId);
		busInvoiceManagement.setIsDelete(1);

		this.update(busInvoiceManagement, updateWrapper);
	}

   @Override
	public BusInvoiceManagementEntity info(String invoiceId){
	   return baseMapper.selectById(invoiceId);
	}
}
