package cn.iocoder.yudao.module.bpm.convert.oa;

import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.expenses.BpmOAExpensesCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.expenses.BpmOAExpensesRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.expenses.Expenses;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAExpensesDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAExpensesItemDO;
import cn.iocoder.yudao.module.smartfactory.api.factoryInfo.dto.FactoryInfoDTO;
import cn.iocoder.yudao.module.system.api.bank.dto.BankRespDTO;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

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

/**
 * 生产开支 Convert
 *
 * @author 符溶馨
 */
@Mapper
public interface BpmOAExpensesConvert {

    BpmOAExpensesConvert INSTANCE = Mappers.getMapper(BpmOAExpensesConvert.class);

    BpmOAExpensesDO convert(BpmOAExpensesCreateReqVO bean);

    BpmOAExpensesRespVO convert(BpmOAExpensesDO bean);

    default BpmOAExpensesRespVO convert1(BpmOAExpensesDO expensesDO, List<BpmOAExpensesItemDO> expensesItemDOs,
                                         Map<Long, FactoryInfoDTO> deptMap, BankRespDTO bankRespDTO) {

        BpmOAExpensesRespVO respVO = BeanUtils.toBean(expensesDO, BpmOAExpensesRespVO.class);

        List<Expenses> expensesItems = BeanUtils.toBean(expensesItemDOs, Expenses.class);
        if (deptMap != null) {

            expensesItems = convertList(expensesItems, deptMap);
        }

        if (bankRespDTO != null) {
            respVO.setBankNo(bankRespDTO.getBankNo());
            respVO.setBankName(bankRespDTO.getBankName());
            respVO.setPayeeName(bankRespDTO.getNickname());
        }

        respVO.setExpensesItem(expensesItems);
        return respVO;
    }

    default List<Expenses> convertList(List<Expenses> list, Map<Long, FactoryInfoDTO> deptMap) {

        return CollectionUtils.convertList(list, expenses -> convert(expenses, deptMap.get(expenses.getDeptId())));
    }

    default Expenses convert(Expenses expensesItem, FactoryInfoDTO dept) {

        if (dept != null) {
            expensesItem.setDeptName(dept.getShortName());
        }

        return expensesItem;
    }
}
