package com.inspect.metadata.alg.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.inspect.base.core.text.Convert;
import com.inspect.base.core.utils.ServletUtils;
import com.inspect.base.core.web.page.TableSupport;
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 java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
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({"/subtype"})
public class BasedataAlgSubtypeController extends BaseController {
    private final IBasedataAlgSubtypeService basedataAlgSubtypeService;
    private final IBasedataAlgService basedataAlgService;

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

    @RequiresPermissions({"basedata:subtype:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(BasedataAlgSubtype basedataAlgSubtype) {
        logger.info("-------参数----basedataAlgSubtype:{}", basedataAlgSubtype);
        if (basedataAlgSubtype != null && StringUtils.isEmpty(basedataAlgSubtype.getAlgName())) {
            this.startPage();
        }
        List<BasedataAlgSubtype> list = this.basedataAlgSubtypeService.selectBasedataAlgSubtypeList(basedataAlgSubtype);
        logger.info("-----------list:{}", list);
        if (null != list && !list.isEmpty()) {
            for (BasedataAlgSubtype bs : list) {
                Long algId = bs.getAlgId();
                if (null != algId) {
                    BasedataAlg alg = this.basedataAlgService.selectBasedataAlgByAlgId(algId);
                    if (null != alg) {
                        bs.setAlgName(alg.getAlgName());
                    } else {
                        bs.setAlgName("");
                    }
                }
            }
            if (basedataAlgSubtype != null && StringUtils.isNotEmpty(basedataAlgSubtype.getAlgName())) {
                logger.info("-----------algName:{}", basedataAlgSubtype.getAlgName());
                list = list.stream().filter(item -> item.getAlgName().contains(basedataAlgSubtype.getAlgName())).collect(Collectors.toList());
                logger.info("-----------list:{}", list);
                int pageNum = Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_NUM), 1);
                int pageSize = Convert.toInt(ServletUtils.getParameter(TableSupport.PAGE_SIZE), 10);
                int fromIndex = (pageNum - 1) * pageSize;
                int toIndex = Math.min(fromIndex + pageSize, list.size());
                List<BasedataAlgSubtype> rowList = list.subList(fromIndex, toIndex);
                logger.info("-----------rowList:{}", rowList);
                TableDataInfo rspData = new TableDataInfo();
                rspData.setCode(200);
                rspData.setRows(rowList);
                rspData.setMsg("Query Ok");
                rspData.setTotal(list.size());
                return rspData;
            }
        }
        return this.getDataTable(list);
    }

    @RequiresPermissions({"basedata:subtype:export"})
    @Log(
            title = "算法附表",
            businessType = BizType.EXPORT
    )
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, BasedataAlgSubtype basedataAlgSubtype) {
        List<BasedataAlgSubtype> list = this.basedataAlgSubtypeService.selectBasedataAlgSubtypeList(basedataAlgSubtype);
        ExcelUtil<BasedataAlgSubtype> util = new ExcelUtil<>(BasedataAlgSubtype.class);
        util.exportExcel(response, list, "算法附表数据");
    }

    @RequiresPermissions({"basedata:subtype:query"})
    @GetMapping({"/{algSubtypeId}"})
    public AjaxResult getInfo(@PathVariable("algSubtypeId") Long algSubtypeId) {
        return AjaxResult.success(this.basedataAlgSubtypeService.selectBasedataAlgSubtypeByAlgSubtypeId(algSubtypeId));
    }

    @RequiresPermissions({"basedata:subtype:add"})
    @Log(
            title = "算法附表",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@RequestBody BasedataAlgSubtype basedataAlgSubtype) {
        int i = this.basedataAlgSubtypeService.checkAlgSubtypeNameIsExit(basedataAlgSubtype.getAlgSubtypeId() + "", basedataAlgSubtype.getAlgId() + "", basedataAlgSubtype.getAlgSubtypeName());
        return i > 0 ? AjaxResult.error("名称已存在！") : this.toAjax(this.basedataAlgSubtypeService.insertBasedataAlgSubtype(basedataAlgSubtype));
    }

    @RequiresPermissions({"basedata:subtype:edit"})
    @Log(
            title = "算法附表",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@RequestBody BasedataAlgSubtype basedataAlgSubtype) {
        int i = this.basedataAlgSubtypeService.checkAlgSubtypeNameIsExit(basedataAlgSubtype.getAlgSubtypeId() + "", basedataAlgSubtype.getAlgId() + "", basedataAlgSubtype.getAlgSubtypeName());
        return i > 0 ? AjaxResult.error("名称已存在！") : this.toAjax(this.basedataAlgSubtypeService.updateBasedataAlgSubtype(basedataAlgSubtype));
    }

    @RequiresPermissions({"basedata:subtype:remove"})
    @Log(
            title = "算法附表",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{algSubtypeIds}"})
    public AjaxResult remove(@PathVariable Long[] algSubtypeIds) {
        return this.toAjax(this.basedataAlgSubtypeService.deleteBasedataAlgSubtypeByAlgSubtypeIds(algSubtypeIds));
    }

    @RequiresPermissions({"basedata:subtype:query"})
    @GetMapping({"/getAlgTypeList"})
    public AjaxResult getAlgTypeList() {
        List<BasedataAlg> list = this.basedataAlgService.getAlgTypeList();
        return AjaxResult.success(JSON.toJSON(list));
    }
}
