/*
*  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 com.youchain.basicdata.service.impl;

import com.youchain.basicdata.domain.RuleConfigure;
import com.youchain.utils.FileUtil;
import com.youchain.utils.PageUtil;
import com.youchain.utils.QueryHelp;
import com.youchain.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import com.youchain.basicdata.repository.RuleConfigureRepository;
import com.youchain.basicdata.service.RuleConfigureService;
import com.youchain.basicdata.service.dto.RuleConfigureDto;
import com.youchain.basicdata.service.dto.RuleConfigureQueryCriteria;
import com.youchain.basicdata.service.mapstruct.RuleConfigureMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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://eladmin.vip
* @description 服务实现
* @author houjianlan
* @date 2024-09-14
**/
@Service
@RequiredArgsConstructor
public class RuleConfigureServiceImpl implements RuleConfigureService {

    private final RuleConfigureRepository ruleConfigureRepository;
    private final RuleConfigureMapper ruleConfigureMapper;

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

    @Override
    public List<RuleConfigureDto> queryAll(RuleConfigureQueryCriteria criteria){
        return ruleConfigureMapper.toDto(ruleConfigureRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RuleConfigureDto findById(Long id) {
        RuleConfigure ruleConfigure = ruleConfigureRepository.findById(id).orElseGet(RuleConfigure::new);
        ValidationUtil.isNull(ruleConfigure.getId(),"RuleConfigure","id",id);
        return ruleConfigureMapper.toDto(ruleConfigure);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RuleConfigureDto create(RuleConfigure resources) {
        return ruleConfigureMapper.toDto(ruleConfigureRepository.save(resources));
    }

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

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

    @Override
    public void download(List<RuleConfigureDto> all, HttpServletResponse response) throws Exception {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RuleConfigureDto ruleConfigure : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("规则类型", ruleConfigure.getRuleType());
            map.put("SQL内容", ruleConfigure.getSqlContent());
            map.put("描述", ruleConfigure.getDescription());
            map.put("是否发邮件", ruleConfigure.getBeyj());
            map.put("邮件备注", ruleConfigure.getYjDes());
            map.put("邮件收件人", ruleConfigure.getYjSjr());
            map.put("是否弹窗", ruleConfigure.getBetc());
            map.put("弹窗消息", ruleConfigure.getTcMsg());
            map.put("颜色标记", ruleConfigure.getBeys());
            map.put("标记颜色值", ruleConfigure.getYsFlg());
            map.put("标记颜色字段", ruleConfigure.getYsField());
            map.put("格式校验正则表达式", ruleConfigure.getGsContent());
            map.put("扩展1标记", ruleConfigure.getBekz1());
            map.put("扩展1消息", ruleConfigure.getKz1Msg());
            map.put("扩展1内容", ruleConfigure.getKz1Content());
            map.put("扩展2标记", ruleConfigure.getBekz2());
            map.put("扩展2消息", ruleConfigure.getKz2Msg());
            map.put("扩展2内容", ruleConfigure.getKz2Content());
            map.put("扩展3标记", ruleConfigure.getBekz3());
            map.put("扩展3消息", ruleConfigure.getKz3Msg());
            map.put("扩展3内容", ruleConfigure.getKz3Content());
            map.put("扩展4标记", ruleConfigure.getBekz4());
            map.put("扩展4消息", ruleConfigure.getKz4Msg());
            map.put("扩展4内容", ruleConfigure.getKz4Content());
            map.put("扩展5标记", ruleConfigure.getBekz5());
            map.put("扩展5消息", ruleConfigure.getKz5Msg());
            map.put("扩展5内容", ruleConfigure.getKz5Content());
            map.put("创建人", ruleConfigure.getCreateBy());
            map.put("更新人", ruleConfigure.getUpdateBy());
            map.put("创建时间", ruleConfigure.getCreateTime());
            map.put("更新时间", ruleConfigure.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}