package com.fjhx.account.service.transaction.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.account.entity.transaction.dto.TransactionDepartmentDto;
import com.fjhx.account.entity.transaction.dto.TransactionDepartmentSelectDto;
import com.fjhx.account.entity.transaction.po.TransactionDepartment;
import com.fjhx.account.entity.transaction.vo.TransactionDepartmentVo;
import com.fjhx.account.mapper.transaction.TransactionDepartmentMapper;
import com.fjhx.account.service.transaction.TransactionDepartmentService;
import com.fjhx.common.entity.corporation.po.Corporation;
import com.fjhx.common.service.corporation.CorporationService;
import com.ruoyi.common.utils.wrapper.IWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 往来单位 服务实现类
 * </p>
 *
 * @author
 * @since 2023-06-26
 */
@Service
public class TransactionDepartmentServiceImpl extends ServiceImpl<TransactionDepartmentMapper, TransactionDepartment> implements TransactionDepartmentService {

    @Autowired
    private CorporationService corporationService;

    @Override
    public List<TransactionDepartmentVo> getList() {
        List<TransactionDepartment> list = this.list();
        List<TransactionDepartmentVo> transactionDepartmentVos = BeanUtil.copyToList(list, TransactionDepartmentVo.class);
        transactionDepartmentVos.forEach(item -> item.setDeptType(0));
        List<Corporation> corporationList = corporationService.list();
        for (Corporation corporation : corporationList) {
            TransactionDepartmentVo transactionDepartmentVo = new TransactionDepartmentVo();
            transactionDepartmentVo.setId(corporation.getId());
            transactionDepartmentVo.setName(corporation.getName());
            transactionDepartmentVo.setDeptType(1);
            transactionDepartmentVos.add(transactionDepartmentVo);
        }
        return transactionDepartmentVos;
    }

    @Override
    public Page<TransactionDepartmentVo> getPage(TransactionDepartmentSelectDto dto) {
        IWrapper<TransactionDepartment> wrapper = getWrapper();
        wrapper.orderByDesc("td", TransactionDepartment::getId);
        Page<TransactionDepartmentVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);
        return page;
    }


    @Override
    public void add(TransactionDepartmentDto transactionDepartmentDto) {
        this.save(transactionDepartmentDto);
    }

    @Override
    public void edit(TransactionDepartmentDto transactionDepartmentDto) {
        this.updateById(transactionDepartmentDto);
    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
