package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.support.json.JSONUtils;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.json.JSON;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
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.GtgsYwUser;
import com.ruoyi.system.service.IGtgsYwUserService;
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;

/**
 * 工商账号管理Controller
 *
 * @author ruoyi
 * @date 2019-12-14
 */
@Controller
@RequestMapping("/system/gtgs_user")
public class GtgsYwUserController extends BaseController {
    private String prefix = "system/gtgs_yw_user";

    @Autowired
    private IGtgsYwUserService gtgsYwUserService;
    @Autowired
    private ISysUserService sysUserService;

    @GetMapping()
    public String user() {
        return prefix + "/user";
    }

    /**
     * 查询工商账号管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GtgsYwUser gtgsYwUser) {
        startPage();
        List<GtgsYwUser> list = gtgsYwUserService.selectGtgsYwUserList(gtgsYwUser);
        return getDataTable(list);
    }

    /**
     * 导出工商账号管理列表
     */
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GtgsYwUser gtgsYwUser) {
        List<GtgsYwUser> list = gtgsYwUserService.selectGtgsYwUserList(gtgsYwUser);
        ExcelUtil<GtgsYwUser> util = new ExcelUtil<GtgsYwUser>(GtgsYwUser.class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增工商账号管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存工商账号管理
     */
    @Log(title = "工商账号管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GtgsYwUser gtgsYwUser) {
        try {
            System.out.println(Global.getConfig("ruoyi.pyip"));
            String result = HttpUtils.sendPost(Global.getConfig("ruoyi.pyip") + "/getuser", "username=" + gtgsYwUser.getUsername() + "&password=" + gtgsYwUser.getPassword());

            System.err.println("result:" + result);
            Map m = (Map) JSONUtils.parse(result);
            if (String.valueOf(m.get("code")).equals("500")) {
                return AjaxResult.warn("账号或密码错误，请重新确认！");
            }
            String userId = (String) m.get("UserId");
            String deptId = (String) m.get("DeptId");
            gtgsYwUser.setUserid(userId);
            gtgsYwUser.setDetpid(deptId);
            //自动关联上工商账户
            SysUser sysUser = ShiroUtils.getSysUser();
            if (!sysUser.getRemark().equals("管理员")) {
                sysUser.setRemark(gtgsYwUser.getUsername());
            }
            sysUserService.updateUserInfo(sysUser);
            System.err.println("result" + result);
            return toAjax(gtgsYwUserService.insertGtgsYwUser(gtgsYwUser));
        } catch (Exception e) {
            return AjaxResult.warn("网络链接错误！");
        }
    }

    /**
     * 修改工商账号管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        GtgsYwUser gtgsYwUser = gtgsYwUserService.selectGtgsYwUserById(id);
        mmap.put("gtgsYwUser", gtgsYwUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存工商账号管理
     */
    @RequiresPermissions("system:user:edit")
    @Log(title = "工商账号管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GtgsYwUser gtgsYwUser) {
        try {
            String result = HttpUtils.sendPost(Global.getConfig("ruoyi.pyip") + "/getuser", "username=" + gtgsYwUser.getUsername() + "&password=" + gtgsYwUser.getPassword());
            System.err.println("result:" + result);
            Map m = (Map) JSONUtils.parse(result);
            if (String.valueOf(m.get("code")).equals("500")) {
                return AjaxResult.warn("账号或密码错误，请重新确认！");
            }
            SysUser sysUser = ShiroUtils.getSysUser();
            if (!sysUser.getRemark().equals("管理员")) {
                sysUser.setRemark(gtgsYwUser.getUsername());
            }
            sysUserService.updateUserInfo(sysUser);
            String userId = (String) m.get("UserId");
            String deptId = (String) m.get("DeptId");
            gtgsYwUser.setUserid(userId);
            gtgsYwUser.setDetpid(deptId);
            return toAjax(gtgsYwUserService.updateGtgsYwUser(gtgsYwUser));
        } catch (Exception e) {
            return AjaxResult.warn("网络链接错误！");
        }
    }

    /**
     * 删除工商账号管理
     */
    @Log(title = "工商账号管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(gtgsYwUserService.deleteGtgsYwUserByIds(ids));
    }
}
