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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.bpm.api.oa.vo.contract.BpmOAContractDTO;
import cn.iocoder.yudao.module.bpm.api.oa.vo.contract.BpmOAContractVO;
import cn.iocoder.yudao.module.bpm.api.oa.vo.contract.ContractStatisticsDTO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.contract.BpmOAContractCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.contract.ContractStatisticsRespVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAContractDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAContractService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * Flowable 流程实例 Api 实现类
 */
@RestController
@Validated
public class BpmOAContractApiImpl implements BpmOAContractApi {

    @Resource
    private BpmOAContractService contractService;

    @Override
    public CommonResult<Long> create(BpmOAContractDTO createReqVO) {

        return success(contractService.createContract(getLoginUserId(), BeanUtils.toBean(createReqVO, BpmOAContractCreateReqVO.class)));
    }

    @Override
    public CommonResult<ContractStatisticsDTO> getContractCount(String relation) {
        ContractStatisticsRespVO respVO = contractService.getContractStatistics(getLoginUserId(), relation);
        return success(BeanUtils.toBean(respVO, ContractStatisticsDTO.class));
    }

    @Override
    public CommonResult<List<ContractStatisticsDTO>> getContractStatistics(List<Long> userIds, LocalDateTime[] createTime) {
        List<ContractStatisticsRespVO> respVOS = contractService.getContractStatisticsListByUserIds(userIds, createTime);
        return success(BeanUtils.toBean(respVOS, ContractStatisticsDTO.class));
    }

    @Override
    public CommonResult<List<BpmOAContractDTO>> getContractList(BpmOAContractVO respVO) {
        List<BpmOAContractDO> contractDO = contractService.getContractList(respVO);
        return success(BeanUtils.toBean(contractDO, BpmOAContractDTO.class));
    }

    @Override
    public void updateSettlementFlagByIds(List<Long> contractIds) {
        contractService.updateSettlementFlagByIds(contractIds);
    }
}
