/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.informationApproval.service.impl;

import me.zhengjie.modules.dm.informationApproval.domain.DmInformationApproval;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.informationApproval.repository.DmInformationApprovalRepository;
import me.zhengjie.modules.dm.informationApproval.service.DmInformationApprovalService;
import me.zhengjie.modules.dm.informationApproval.service.dto.DmInformationApprovalDto;
import me.zhengjie.modules.dm.informationApproval.service.dto.DmInformationApprovalQueryCriteria;
import me.zhengjie.modules.dm.informationApproval.service.mapstruct.DmInformationApprovalMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author xwx
* @date 2021-04-23
**/
@Service
@RequiredArgsConstructor
public class DmInformationApprovalServiceImpl implements DmInformationApprovalService {

    private final DmInformationApprovalRepository dmInformationApprovalRepository;
    private final DmInformationApprovalMapper dmInformationApprovalMapper;

    @Override
    public Map<String,Object> queryAll(DmInformationApprovalQueryCriteria criteria, Pageable pageable){
        Page<DmInformationApproval> page = dmInformationApprovalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(dmInformationApprovalMapper::toDto));
    }

    @Override
    public List<DmInformationApprovalDto> queryAll(DmInformationApprovalQueryCriteria criteria){
        return dmInformationApprovalMapper.toDto(dmInformationApprovalRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmInformationApprovalDto findById(Long id) {
        DmInformationApproval dmInformationApproval = dmInformationApprovalRepository.findById(id).orElseGet(DmInformationApproval::new);
        ValidationUtil.isNull(dmInformationApproval.getId(),"DmInformationApproval","id",id);
        return dmInformationApprovalMapper.toDto(dmInformationApproval);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmInformationApprovalDto create(DmInformationApproval resources) {
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);
        resources.setId(snowflake.nextId()); 
        return dmInformationApprovalMapper.toDto(dmInformationApprovalRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DmInformationApproval resources) {
        DmInformationApproval dmInformationApproval = dmInformationApprovalRepository.findById(resources.getId()).orElseGet(DmInformationApproval::new);
        ValidationUtil.isNull( dmInformationApproval.getId(),"DmInformationApproval","id",resources.getId());
        dmInformationApproval.copy(resources);
        dmInformationApprovalRepository.save(dmInformationApproval);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            dmInformationApprovalRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DmInformationApprovalDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmInformationApprovalDto dmInformationApproval : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("信息主键", dmInformationApproval.getInformation().getId());
            map.put("审批人", dmInformationApproval.getApprovalUser());
            map.put("(00-待审核  01-审批通过(已发布） 02-审核未通过)", dmInformationApproval.getStatus());
            map.put("审批备注", dmInformationApproval.getApprovalRemark());
            map.put("是否为最新审批", dmInformationApproval.getIsNew());
            map.put("任务ID act_ru_task", dmInformationApproval.getTaskId());
            map.put("任务实例id act_ru_execution", dmInformationApproval.getProcInstId());
            map.put("创建者", dmInformationApproval.getCreateBy());
            map.put("更新者", dmInformationApproval.getUpdateBy());
            map.put("创建日期", dmInformationApproval.getCreateTime());
            map.put("更新时间", dmInformationApproval.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void reset(Long infoId) {
        dmInformationApprovalRepository.reset(infoId);
    }

    @Override
    public void resetTask(Long infoId, String approvalUser, String taskId, String procInstId) {
        dmInformationApprovalRepository.resetTask(infoId,approvalUser,taskId,procInstId);
    }

    @Override
    public List<DmInformationApproval> findInfoByReqAndUser(Long infoId, String approvalUser) {
        return dmInformationApprovalRepository.findInfoByReqAndUser(infoId,approvalUser);
    }


}