package com.ruoyi.web.controller.nanjing;

import java.util.*;

import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.nanjing.domain.TBdLinetype;
import com.ruoyi.nanjing.domain.TBdProductinfo;
import com.ruoyi.nanjing.domain.TBdStationtype;
import com.ruoyi.nanjing.service.ITBdLinetypeService;
import com.ruoyi.nanjing.service.ITBdStationtypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.nanjing.domain.TBdSubstation;
import com.ruoyi.nanjing.service.ITBdSubstationService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 工位管理Controller
 * 
 * @author limy
 * @date 2021-01-22
 */
@Controller
@RequestMapping("/nanjing/SubStation")
public class TBdSubstationController extends BaseController
{
    private String prefix = "nanjing/SubStation";

    @Autowired
    private ITBdSubstationService tBdSubstationService;
    @Autowired
    private ITBdLinetypeService linetypeService;
    @Autowired
    private ITBdStationtypeService stationtypeService;

    @RequiresPermissions("nanjing:SubStation:view")
    @GetMapping()
    public String SubStation(ModelMap map)
    {
        List<TBdLinetype> linetypes = linetypeService.selectTBdLinetypeList(new TBdLinetype());
        List<TBdStationtype> stationtypes = stationtypeService.selectTBdStationtypeList(new TBdStationtype());
        map.addAttribute("linename",linetypes);
        map.addAttribute("stationtype",stationtypes);
        return prefix + "/SubStation";
    }

    /**
     * 查询工位管理列表
     */
    @RequiresPermissions("nanjing:SubStation:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TBdSubstation tBdSubstation)
    {
        startPage();
        List<TBdSubstation> list = tBdSubstationService.selectTBdSubstationList(tBdSubstation);
        return getDataTable(list);
    }

    /**
     * 加载工位列表树
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = tBdSubstationService.selectTbdStationTree(new TBdSubstation());
        return ztrees;
    }
    /**
     * 导出工位管理列表
     */
    @RequiresPermissions("nanjing:SubStation:export")
    @Log(title = "工位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TBdSubstation tBdSubstation)
    {
        List<TBdSubstation> list = tBdSubstationService.selectTBdSubstationList(tBdSubstation);
        ExcelUtil<TBdSubstation> util = new ExcelUtil<TBdSubstation>(TBdSubstation.class);
        return util.exportExcel(list, "SubStation");
    }

    /**
     * 新增工位管理
     */
    @GetMapping("/add")
    public String add(ModelMap map)
    {
        //工位序号+
        List<TBdSubstation> list = tBdSubstationService.selectStationcode();
        int max;
        if(list.size()==0){
            max = 0;
            List<TBdSubstation> lists = new ArrayList<>();
            TBdSubstation tBdSubstation = new TBdSubstation();
            tBdSubstation.setStationID(max+1L);
            lists.add(tBdSubstation);
            map.addAttribute("si",lists);
        }else{
            max = Convert.toInt(list.get(0).getStationID());
            list.get(0).setStationID(max+1L);
            map.addAttribute("si",list);
        }
        //排序id+
        List<TBdSubstation> list1 = tBdSubstationService.selectOrderid();
        int max1;
        if(list1.size()==0){
            max1 = 0;
            List<TBdSubstation> list2 = new ArrayList<>();
            TBdSubstation tBdSubstation = new TBdSubstation();
            tBdSubstation.setOrderID(max1+1L);
            list2.add(tBdSubstation);
            map.addAttribute("oi",list2);
        }else{
            max1 = Convert.toInt(list.get(0).getOrderID());
            list1.get(0).setOrderID(max1+1L);
            map.addAttribute("oi",list1);
        }
        List<TBdLinetype> linetypes = linetypeService.selectTBdLinetypeList(new TBdLinetype());
        List<TBdStationtype> stationtypes = stationtypeService.selectTBdStationtypeList(new TBdStationtype());
        map.addAttribute("linename",linetypes);
        map.addAttribute("stationtype",stationtypes);
        return prefix + "/add";
    }

    /**
     * 新增保存工位管理
     */
    @RequiresPermissions("nanjing:SubStation:add")
    @Log(title = "工位管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TBdSubstation tBdSubstation)
    {
        String res = "";
        Map map = new HashMap<String,Object>();
        map.put("tableName",tBdSubstation.getTableName());
        map.put("ParaAmount",tBdSubstation.getParaCount());
        String msg = tBdSubstationService.createTable(map);

        tBdSubstation.setUniteID(0L);
        tBdSubstation.setUserID(ShiroUtils.getUserId().toString());
        int insertStation = tBdSubstationService.insertTBdSubstation(tBdSubstation);
        if(insertStation>0){
            res = "工位创建成功;"+msg;
        }else {
            res = "工位创建失败；"+msg;
        }
        return AjaxResult.success(res);
    }

    /**
     * 修改工位管理
     */
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        List<TBdLinetype> linetypes = linetypeService.selectTBdLinetypeList(new TBdLinetype());
        List<TBdStationtype> stationtypes = stationtypeService.selectTBdStationtypeList(new TBdStationtype());
        mmap.addAttribute("linename",linetypes);
        mmap.addAttribute("stationtype",stationtypes);
        TBdSubstation tBdSubstation = tBdSubstationService.selectTBdSubstationById(ID);
        mmap.put("tBdSubstation", tBdSubstation);
        return prefix + "/edit";
    }

    @GetMapping("/editParam/{ID}")
    public String editParam(@PathVariable("ID") Long ID,ModelMap modelMap)
    {
        TBdSubstation tBdSubstation = tBdSubstationService.selectTBdSubstationById(ID);
        modelMap.put("tBdSubstation", tBdSubstation);
        return prefix + "/editParam";
    }

    /**
     * 修改保存工位管理
     */
    @RequiresPermissions("nanjing:SubStation:edit")
    @Log(title = "工位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TBdSubstation tBdSubstation)
    {
        tBdSubstation.setUpdateTime(new Date());
        Map map = new HashMap<String,Object>();
        map.put("paraCount",tBdSubstation.getParaCount());
        map.put("tableName",tBdSubstation.getTableName());
        map.put("stationID",tBdSubstation.getStationID());
        tBdSubstationService.updateTable(map);
        return toAjax(tBdSubstationService.updateTBdSubstation(tBdSubstation));
    }

    /**
     * 删除工位管理
     */
    @RequiresPermissions("nanjing:SubStation:remove")
    @Log(title = "工位管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        TBdSubstation substation = tBdSubstationService.selectTBdSubstationById(Convert.toLong(ids));
        Map map = new HashMap<String,Object>();
        map.put("stationID",substation.getStationID());
        tBdSubstationService.deletePara(map);
//        tBdSubstationService.deleteTBdSubstationByIds(ids)
        return toAjax(1);
    }
}
