package com.inspect.metadata.alg.controller;

import com.alibaba.fastjson.JSON;
import com.inspect.metadata.alg.domain.BasedataAlg;
import com.inspect.metadata.alg.domain.BasedataAlgSubtype;
import com.inspect.metadata.alg.service.IBasedataAlgService;
import com.inspect.metadata.alg.service.IBasedataAlgSubtypeService;
import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/alg"})
public class BasedataAlgController extends BaseController {
    private final IBasedataAlgService basedataAlgService;
    private final IBasedataAlgSubtypeService basedataAlgSubtypeService;

    public BasedataAlgController(IBasedataAlgService basedataAlgService, IBasedataAlgSubtypeService basedataAlgSubtypeService) {
        this.basedataAlgService = basedataAlgService;
        this.basedataAlgSubtypeService = basedataAlgSubtypeService;
    }

    @RequiresPermissions({"basedata:alg:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(BasedataAlg basedataAlg) {
        this.startPage();
        List<BasedataAlg> list = this.basedataAlgService.selectBasedataAlgList(basedataAlg);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"basedata:alg:export"})
    @Log(
            title = "算法",
            businessType = BizType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, BasedataAlg basedataAlg) {
        List<BasedataAlg> list = this.basedataAlgService.selectBasedataAlgList(basedataAlg);
        ExcelUtil<BasedataAlg> util = new ExcelUtil<>(BasedataAlg.class);
        util.exportExcel(response, list, "算法数据");
    }

    @RequiresPermissions({"basedata:alg:query"})
    @GetMapping({"/{algId}"})
    public AjaxResult getInfo(@PathVariable("algId") Long algId) {
        return AjaxResult.success(this.basedataAlgService.selectBasedataAlgByAlgId(algId));
    }

    @RequiresPermissions({"basedata:alg:add"})
    @Log(
            title = "算法",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@RequestBody BasedataAlg basedataAlg) {
        int i = this.basedataAlgService.checkAlgNameIsExist(basedataAlg.getAlgName(), basedataAlg.getAlgId() + "");
        if (i > 0) {
            return AjaxResult.error("名称已存在!");
        } else {
            basedataAlg.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.basedataAlgService.insertBasedataAlg(basedataAlg));
        }
    }

    @RequiresPermissions({"basedata:alg:edit"})
    @Log(
            title = "算法",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody BasedataAlg basedataAlg) {
        int i = this.basedataAlgService.checkAlgNameIsExist(basedataAlg.getAlgName(), basedataAlg.getAlgId().toString());
        return i > 0 ? AjaxResult.error("名称已存在!") : this.toAjax(this.basedataAlgService.updateBasedataAlg(basedataAlg));
    }

    @RequiresPermissions({"basedata:alg:remove"})
    @Log(
            title = "算法",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{algIds}"})
    public AjaxResult remove(@PathVariable Long[] algIds) {
        return this.toAjax(this.basedataAlgService.deleteBasedataAlgByAlgIds(algIds));
    }

    @RequiresPermissions({"basedata:alg:query"})
    @GetMapping({"/getAlgTypeList"})
    public AjaxResult getAlgTypeList() {
        List<BasedataAlg> basedataAlgList = this.basedataAlgService.selectBasedataAlgTypeList();
        List<BasedataAlg> basedataAlgListNew = new ArrayList<>();
        List<Object> list = new ArrayList<>();
        Iterator<BasedataAlg> iterator = basedataAlgList.iterator();
        BasedataAlg basedataAlg;
        while (iterator.hasNext()) {
            basedataAlg = iterator.next();
            if (!list.contains(basedataAlg.getAlgId())) {
                list.add(basedataAlg.getAlgId());
                basedataAlgListNew.add(basedataAlg);
            }
        }

        iterator = basedataAlgListNew.iterator();
        while (iterator.hasNext()) {
            basedataAlg = iterator.next();
            BasedataAlgSubtype basedataAlgSubtype = new BasedataAlgSubtype();
            basedataAlgSubtype.setAlgId(basedataAlg.getAlgId());
            List<BasedataAlgSubtype> basedataAlgSubtypeList = this.basedataAlgSubtypeService.selectBasedataAlgSubtypeList(basedataAlgSubtype);
            basedataAlg.setBasedataAlgSubtypeList(basedataAlgSubtypeList);
        }

        return AjaxResult.success(JSON.toJSON(basedataAlgListNew));
    }
}
