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

import me.zhengjie.modules.dm.service.domain.DmServicePj;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.dm.service.repository.DmServicePjRepository;
import me.zhengjie.modules.dm.service.service.DmServicePjService;
import me.zhengjie.modules.dm.service.service.dto.DmServicePjDto;
import me.zhengjie.modules.dm.service.service.dto.DmServicePjQueryCriteria;
import me.zhengjie.modules.dm.service.service.mapstruct.DmServicePjMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
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 sunmz
* @date 2021-09-15
**/
@Service
@RequiredArgsConstructor
public class DmServicePjServiceImpl implements DmServicePjService {

    private final DmServicePjRepository dmServicePjRepository;
    private final DmServicePjMapper dmServicePjMapper;

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

    @Override
    public List<DmServicePjDto> queryAll(DmServicePjQueryCriteria criteria){
        return dmServicePjMapper.toDto(dmServicePjRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public DmServicePjDto findById(Integer id) {
        DmServicePj dmServicePj = dmServicePjRepository.findById(id).orElseGet(DmServicePj::new);
        ValidationUtil.isNull(dmServicePj.getId(),"DmServicePj","id",id);
        return dmServicePjMapper.toDto(dmServicePj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmServicePjDto create(DmServicePj resources) {
        return dmServicePjMapper.toDto(dmServicePjRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DmServicePjDto createApp(DmServicePj resources) {
        Map<String, Object> map = dmServicePjRepository.enableApp();
        resources.setEnabled(map.get("enabled").toString());
        return dmServicePjMapper.toDto(dmServicePjRepository.save(resources));
    }

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

    @Override
    public void updatePj(String resources) {
//        for (DmServicePj dmServicePj: resources){
            dmServicePjRepository.updatePj(resources);
//        }
    }

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

    @Override
    public void download(List<DmServicePjDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DmServicePjDto dmServicePj : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("评价用户", dmServicePj.getUserName());
            map.put("用户工号", dmServicePj.getUserCode());
            map.put("综合评分", dmServicePj.getTotalPoint());
            map.put("评价时间", dmServicePj.getPjTime());
            map.put("味道", dmServicePj.getWdPoint());
            map.put("品种", dmServicePj.getPzPoint());
            map.put("分量", dmServicePj.getFlPoint());
            map.put("服务态度", dmServicePj.getFwtdPoint());
            map.put("环境卫生", dmServicePj.getHjwsPoint());
            map.put("综合服务", dmServicePj.getZhfwPoint());
            map.put("持续改进", dmServicePj.getCxgjPoint());
            map.put("意见建议", dmServicePj.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public int servicePjApp(String userCode) {
        return dmServicePjRepository.servicePjApp(userCode);
    }

    @Override
    public Map<String,Object> enableApp() {
        return dmServicePjRepository.enableApp();
    }

    @Override
    public Map<String, Object> getDmServicePjByUserCodeAndPjTime(String userCode) {
        return dmServicePjRepository.getDmServicePjByUserCodeAndPjTime(userCode);
    }
}