/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.security.trans.projectBudgetManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Preconditions;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.security.common.Constant;
import org.security.ht.entity.basic.Department;
import org.security.trans.basic.service.IDepartmentService;
import org.security.trans.projectBudgetManagement.dto.BudgetItemDTO;
import org.security.trans.projectBudgetManagement.entity.BudgetItem;
import org.security.trans.projectBudgetManagement.vo.BudgetItemVO;
import org.security.trans.projectBudgetManagement.mapper.BudgetItemMapper;
import org.security.trans.projectBudgetManagement.service.IBudgetItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.security.trans.projectBudgetManagement.vo.DictDataVO;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.util.List;

/**
 * 项目管理-预算管理信息表 服务实现类
 *
 * @author BladeX
 * @since 2022-08-05
 */
@Service
@Slf4j
@AllArgsConstructor
public class BudgetItemServiceImpl extends ServiceImpl<BudgetItemMapper, BudgetItem> implements IBudgetItemService {

	private final IDepartmentService departmentService;
	@Override
	public IPage<BudgetItemVO> selectBudgetItemPage(IPage<BudgetItemVO> page, BudgetItemDTO budgetItemDTO) {
		log.info("==============获取预算信息列表页数据开始==============");
		List<BudgetItemVO> vos = baseMapper.selectBudgetItemPage(page, budgetItemDTO);
		if (Func.isNotEmpty(vos)){
			vos.forEach(item ->{
				Department department = departmentService.getById(item.getBudgetOrg());
				item.setBudgetOrg(Func.isNotEmpty(department) ? department.getDeptName() : null);
				item.setBudgetType(baseMapper.getDictName(item.getBudgetType()));
				item.setBudgetKind(baseMapper.getDictName(item.getBudgetKind()));
			});
		}
		log.info("==============获取预算信息列表页数据结束==============");
		return page.setRecords(vos);
	}

	@Override
	public boolean saveData(BudgetItemDTO budgetItemDTO) {
		log.info("==============保存/更新预算信息数据开始==============");
		BudgetItem budgetItem = Func.copy(budgetItemDTO, BudgetItem.class);
		Preconditions.checkArgument(Func.isNotEmpty(budgetItem),"预算信息为空");
		budgetItem.setDeptId(AuthUtil.getDeptId());
		budgetItem.setDeptName(departmentService.getById(AuthUtil.getDeptId()).getDeptName());
		boolean b = this.saveOrUpdate(budgetItem);
		log.info("==============保存/更新预算信息数据结束==============");
		return b;
	}

	@Override
	public BudgetItemVO getData(String id) {
		log.info("==============获取预算详情信息数据开始==============");
		BudgetItem budgetItem = this.getById(id);
		log.info("==============获取预算详情信息数据结束==============");
		return Func.copy(budgetItem, BudgetItemVO.class);
	}

	@Override
	public List<DictDataVO> getDictData(String keyName) {
		return baseMapper.getDictData(keyName);
	}

	@Override
	public List<BudgetItemVO> getParentBudget(String deptId) {
		log.info("==============获取父级预算信息数据开始==============");
		List<BudgetItem> budgetItemList = this.list(new QueryWrapper<BudgetItem>().lambda().eq(Func.isNotEmpty(deptId),BudgetItem::getDeptId, deptId)
			.eq(BudgetItem::getState,Constant.SUBMIT_TYPE_ONE));
		log.info("==============获取父级预算信息数据结束==============");
		return Func.copy(budgetItemList, BudgetItemVO.class);
	}

}
