package com.pdaaphm.biz.controller;

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

import com.pdaaphm.biz.dto.AlgorithmDTO;
import com.pdaaphm.common.core.domain.entity.SysDictData;
import com.pdaaphm.common.utils.StringUtils;
import com.pdaaphm.system.service.ISysDictTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.pdaaphm.common.annotation.Log;
import com.pdaaphm.common.core.controller.BaseController;
import com.pdaaphm.common.core.domain.AjaxResult;
import com.pdaaphm.common.enums.BusinessType;
import com.pdaaphm.biz.domain.Algorithm;
import com.pdaaphm.biz.service.IAlgorithmService;
import com.pdaaphm.common.utils.poi.ExcelUtil;
import com.pdaaphm.common.core.page.TableDataInfo;

/**
 * 算法Controller
 *
 * @author xlk
 * @date 2023-07-26
 */
@RestController
@RequestMapping("/algoManager/algorithm")
public class AlgorithmController extends BaseController
{
    @Autowired
    private IAlgorithmService algorithmService;

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 查询算法列表
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:list')")
    @GetMapping("/list")
    public TableDataInfo list(Algorithm algorithm)
    {
        startPage();
        if (StringUtils.isNotEmpty(algorithm.getDictType())){
            List<SysDictData> sysDictDataList = sysDictTypeService.selectDictDataByType(algorithm.getDictType());
            algorithm.setTypes(sysDictDataList.stream().map(SysDictData::getDictValue).collect(Collectors.toList()));
        }
        List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
        return getDataTable(list);
    }

    /**
     * 导出算法列表
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:export')")
    @Log(title = "算法", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Algorithm algorithm)
    {
        List<Algorithm> list = algorithmService.selectAlgorithmList(algorithm);
        ExcelUtil<Algorithm> util = new ExcelUtil<Algorithm>(Algorithm.class);
        util.exportExcel(response, list, "算法数据");
    }

    /**
     * 获取算法详细信息
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(algorithmService.selectAlgorithmById(id));
    }

    /**
     * 获取算法对象详细信息
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:query')")
    @GetMapping(value = "/getAlgorithmDto/{id}")
    public AjaxResult getAlgorithmDto(@PathVariable("id") Long id)
    {
        return success(algorithmService.getAlgorithmDto(id));
    }

    /**
     * 新增算法
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:add')")
    @Log(title = "算法", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Algorithm algorithm)
    {
        return toAjax(algorithmService.insertAlgorithm(algorithm));
    }

    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:add')")
    @Log(title = "算法", businessType = BusinessType.INSERT)
    @PostMapping("/addOrUpdateDto")
    public AjaxResult addDto(@RequestBody AlgorithmDTO algorithm)
    {
        return toAjax(algorithmService.addOrUpdateDto(algorithm));
    }


    /**
     * 修改算法
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:edit')")
    @Log(title = "算法", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Algorithm algorithm)
    {
        return toAjax(algorithmService.updateAlgorithm(algorithm));
    }

    /**
     * 删除算法
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:remove')")
    @Log(title = "算法", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(algorithmService.deleteAlgorithmByIds(ids));
    }

    @GetMapping("/getOption")
    public AjaxResult getOption() { return success(algorithmService.getOption()); }

    /**
     * 运行算法
     */
    @PreAuthorize("@ss.hasPermi('algoManager:algorithm:edit')")
    @Log(title = "算法", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/runAlgorithms/{id}")
    public AjaxResult runAlgorithms(@PathVariable("id") Long id) throws IOException {
        algorithmService.runAlgorithms(id);
        return success();
    }
}
