package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.system.domain.OrhonGtxx;
import com.ruoyi.system.domain.SysDictData;
import com.ruoyi.system.domain.SysDictType;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IOrhonGtxxService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
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.*;

import java.io.File;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;

/**
 * 个体信息Controller
 * 
 * @author ruoyi
 * @date 2020-07-27
 */
@Controller
@RequestMapping("/system/gtxx")
public class OrhonGtxxController extends BaseController
{
    private String prefix = "system/gtxx";

    @Autowired
    private IOrhonGtxxService orhonGtxxService;

    @RequiresPermissions("system:gtxx:view")
    @GetMapping()
    public String gtxx()
    {
        return prefix + "/gtxx";
    }

    /**
     * 查询个体信息列表
     */
    @RequiresPermissions("system:gtxx:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrhonGtxx orhonGtxx)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        startPage();
        if ( !sysUser.getLoginName().equals("admin") ) {
            orhonGtxx.setDeptId(sysUser.getUserDeptId());
        }

        List<OrhonGtxx> list = orhonGtxxService.selectOrhonGtxxList(orhonGtxx);
        return getDataTable(list);
    }

    /**
     * 导出个体信息列表
     */
    @RequiresPermissions("system:gtxx:export")
    @Log(title = "个体信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrhonGtxx orhonGtxx)
    {
        List<OrhonGtxx> list = orhonGtxxService.selectOrhonGtxxList(orhonGtxx);
        ExcelUtil<OrhonGtxx> util = new ExcelUtil<OrhonGtxx>(OrhonGtxx.class);
        return util.exportExcel(list, "gtxx");
    }

    /**
     * 新增个体信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存个体信息
     */
    @RequiresPermissions("system:gtxx:add")
    @Log(title = "个体信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OrhonGtxx orhonGtxx)
    {
        orhonGtxx.setId( java.util.UUID.randomUUID().toString() );
        return toAjax(orhonGtxxService.insertOrhonGtxx(orhonGtxx));
    }

    /**
     * 修改个体信息
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        OrhonGtxx orhonGtxx = orhonGtxxService.selectOrhonGtxxById(id);
        mmap.put("orhonGtxx", orhonGtxx);
        return prefix + "/edit";
    }

    /**
     * 修改保存个体信息
     */
    @RequiresPermissions("system:gtxx:edit")
    @Log(title = "个体信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrhonGtxx orhonGtxx)
    {
        return toAjax(orhonGtxxService.updateOrhonGtxx(orhonGtxx));
    }

    /**
     * 删除个体信息
     */
    @RequiresPermissions("system:gtxx:remove")
    @Log(title = "个体信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orhonGtxxService.deleteOrhonGtxxByIds(ids));
    }

    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private ISysDictDataService dictDataService;

    @GetMapping("/dict")
    public void dict() throws Exception{
        SysDictType sysDictType = new SysDictType();

        sysDictType.setDictType("gmjjhydm");
        sysDictType.setDictName("国民经济行业代码");
        sysDictType.setStatus("0");
        sysDictType.setCreateBy("admin");
        sysDictType.setCreateTime(new Date());
        sysDictType.setRemark("CA06国民经济行业代码");

        this.dictTypeService.insertDictType(sysDictType);


        List<String> lines = FileUtils.readLines(new File("/Users/ebusu/workspace/orhon/fgqyxxlr/dict/dict1.txt"), Charset.forName("UTF-8"));
        long index = 1;
        for (String line : lines) {

            String[] lineData = line.split(",");

            SysDictData sysDictData = new SysDictData();

            sysDictData.setDictSort(Long.parseLong(lineData[0]));
            sysDictData.setDictLabel(lineData[2]);
            sysDictData.setDictValue(lineData[1]);
            sysDictData.setDictType(sysDictType.getDictType());
            sysDictData.setIsDefault("N");
            sysDictData.setStatus("0");
            sysDictData.setCreateBy("admin");
            sysDictData.setCreateTime(new Date());

            dictDataService.insertDictData(sysDictData);
        }
    }

}
