package com.izouma.zhirongip.web.supply;

import cn.hutool.core.convert.Convert;
import com.izouma.zhirongip.domain.supply.TechnicalManager;
import com.izouma.zhirongip.dto.PageQuery;
import com.izouma.zhirongip.exception.BusinessException;
import com.izouma.zhirongip.repo.supply.TechnicalManagerRepo;
import com.izouma.zhirongip.service.supply.TechnicalManagerService;
import com.izouma.zhirongip.utils.ObjUtils;
import com.izouma.zhirongip.utils.SecurityUtils;
import com.izouma.zhirongip.utils.excel.ExcelUtils;
import com.izouma.zhirongip.web.BaseController;
import io.swagger.annotations.ApiOperation;
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;

@RestController
@RequestMapping("/technicalManager")
@AllArgsConstructor
public class TechnicalManagerController extends BaseController {
    private final TechnicalManagerService technicalManagerService;
    private final TechnicalManagerRepo    technicalManagerRepo;


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


    //@PreAuthorize("hasRole('ADMIN')")
    @PostMapping("/all")
    public Page<TechnicalManager> 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 technicalManagerService.all(pageQuery, field);
    }

    @GetMapping("/get/{id}")
    public TechnicalManager get(@PathVariable Long id) {
        return technicalManagerService.get(id);
    }

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

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

    @ApiOperation("相关推荐")
    @GetMapping("/recommend")
    public Page<TechnicalManager> recommend(@RequestParam Long id) {
        return technicalManagerService.recommend(id);
    }

    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("审核")
    @PostMapping("/audit")
    public void audit(@RequestParam Long id, @RequestParam boolean pass, String remark) {
        technicalManagerService.audit(id, pass, remark);
    }
}

