package com.izouma.zhirongip.web.supply;

import cn.hutool.core.convert.Convert;
import com.izouma.zhirongip.domain.Setting;
import com.izouma.zhirongip.repo.SettingRepo;
import com.izouma.zhirongip.utils.SecurityUtils;
import com.izouma.zhirongip.web.BaseController;
import com.izouma.zhirongip.domain.supply.ServiceProvider;
import com.izouma.zhirongip.service.supply.ServiceProviderService;
import com.izouma.zhirongip.dto.PageQuery;
import com.izouma.zhirongip.exception.BusinessException;
import com.izouma.zhirongip.repo.supply.ServiceProviderRepo;
import com.izouma.zhirongip.utils.ObjUtils;
import com.izouma.zhirongip.utils.excel.ExcelUtils;

import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/serviceProvider")
@AllArgsConstructor
public class ServiceProviderController extends BaseController {
    private final ServiceProviderService serviceProviderService;
    private final ServiceProviderRepo    serviceProviderRepo;
    private final SettingRepo            settingRepo;

    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/save")
    public ServiceProvider save(@RequestBody ServiceProvider record) {
        if (record.getId() != null) {
            ServiceProvider orig = serviceProviderRepo.findById(record.getId())
                    .orElseThrow(new BusinessException("无记录"));
            ObjUtils.merge(orig, record);
            return serviceProviderRepo.save(orig);
        }
        record.setUserId(SecurityUtils.getAuthenticatedUser().getId());
        return serviceProviderRepo.save(record);
    }


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<ServiceProvider> all(@RequestBody PageQuery pageQuery) {
        Map<String, Object> query = pageQuery.getQuery();
        Object fieldObj = query.get("field");
        Long field = Convert.convert(Long.class, fieldObj);
        query.remove("field");

        return serviceProviderService.all(pageQuery, field);
    }

    @GetMapping("/get/{id}")
    public ServiceProvider get(@PathVariable Long id) {
        ServiceProvider record = serviceProviderRepo.findById(id).orElseThrow(new BusinessException("无记录"));
        Map<Long, String> settingMap = settingRepo.findAllById(record.getField())
                .stream()
                .collect(Collectors.toMap(Setting::getId, Setting::getName));
        record.setFieldName(record.getField().stream().map(settingMap::get).collect(Collectors.toList()));
        return record;
    }

    @PostMapping("/del/{id}")
    public void del(@PathVariable Long id) {
        serviceProviderRepo.softDelete(id);
    }

    @GetMapping("/excel")
    @ResponseBody
    public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
        List<ServiceProvider> data = all(pageQuery).getContent();
        ExcelUtils.export(response, data);
    }
}

