package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.redis.RedisUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.IYtjService;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
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.system.domain.GtgsywOld;
import com.ruoyi.system.service.IGtgsywOldService;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 个体字号Controller
 *
 * @author xu
 * @date 2019-12-04
 */
@Controller
@RequestMapping("/system/zwzh")
public class GtgsywOldController extends BaseController
{
    private String prefix = "system/gtzwzh";

    @Autowired
    private IGtgsywOldService gtgsywOldService;
    @Autowired
    private IYtjService iYtjService;
    @Resource
    private RedisUtil redisUtil;

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



    /**
     * 查询个体字号列表
     */
    @RequiresPermissions("system:zwzh:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GtgsywOld gtgsywOld)
    {
        startPage();
        List<GtgsywOld> list = gtgsywOldService.selectGtgsywOldList(gtgsywOld);
        return getDataTable(list);
    }

    /**
     * 导出个体字号列表
     */
    @RequiresPermissions("system:zwzh:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GtgsywOld gtgsywOld)
    {
        List<GtgsywOld> list = gtgsywOldService.selectGtgsywOldList(gtgsywOld);
        ExcelUtil<GtgsywOld> util = new ExcelUtil<GtgsywOld>(GtgsywOld.class);
        return util.exportExcel(list, "zwzh");

    }

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

    /**
     * 新增保存个体字号
     */
    @RequiresPermissions("system:zwzh:add")
    @Log(title = "个体字号", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GtgsywOld gtgsywOld)
    {
        return toAjax(gtgsywOldService.insertGtgsywOld(gtgsywOld));
    }

    /**
     * 修改个体字号
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GtgsywOld gtgsywOld = gtgsywOldService.selectGtgsywOldById(id);
        mmap.put("gtgsywOld", gtgsywOld);
        return prefix + "/edit";
    }

    /**
     * 修改保存个体字号
     */
    @RequiresPermissions("system:zwzh:edit")
    @Log(title = "个体字号", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GtgsywOld gtgsywOld)
    {
        return toAjax(gtgsywOldService.updateGtgsywOld(gtgsywOld));
    }

    /**
     * 删除个体字号
     */
    @RequiresPermissions("system:zwzh:remove")
    @Log(title = "个体字号", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        return toAjax(gtgsywOldService.deleteGtgsywOldByIds(ids));
    }
    /**
     * 删除个体字号
     */
    @Log(title = "个体字号", businessType = BusinessType.DELETE)
    @PostMapping( "/removeone")
    @ResponseBody
    public AjaxResult removeone(String ids)
    {

        GtgsywOld gto= gtgsywOldService.selectGtgsywOldById(Long.valueOf(ids));
        redisUtil.del(gto.getHangye()+":"+ToPinyin(gto.getZwzh().replace("", "-").trim()));
        System.err.println(gto.getHangye()+":"+ToPinyin(gto.getZwzh().replace("", "-").trim()));
        return toAjax(gtgsywOldService.deleteGtgsywOldById(Long.valueOf(ids)));
    }

    /**
     * excel表 导入到sql中
      * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<GtgsywOld> util = new ExcelUtil<GtgsywOld>(GtgsywOld.class);
        List<GtgsywOld> gtgsywOlds = util.importExcel(file.getInputStream());
        String message = gtgsywOldService.importOld(gtgsywOlds, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public static String ToPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }



}
