package com.ruoyi.interfaces.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.interfaces.core.page.Page;
import com.ruoyi.interfaces.core.page.PageUtils;
import com.ruoyi.interfaces.domain.PtAppUser;
import com.ruoyi.interfaces.service.PtAppUserService;
import com.ruoyi.system.mapper.SysUserMapper;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

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


/**
 * @author weking
 * @date 2018-11-6
 */
@RestController
@RequestMapping("/pt/app/user")
public class PtAppUserController extends BaseController {

    @Autowired
    private PtAppUserService ptAppUserService;

    private final SysUserMapper sysUserMapper;

    public PtAppUserController(SysUserMapper sysUserMapper) {
        this.sysUserMapper = sysUserMapper;
    }

    @ApiOperation("添加数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public AjaxResult insert(PtAppUser ptAppUser) {
        ptAppUser.setUserRegdate(new Date());
        ptAppUser.setUserStatus("1");// 默认可以登录系统
        int ret = ptAppUserService.insert(ptAppUser);
        return ret > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("添加数据")
    @PostMapping
    public AjaxResult save(@RequestBody PtAppUser ptAppUser) {
        return AjaxResult.check(ptAppUserService.save(ptAppUser) > 0);
    }

    @ApiOperation("添加数据")
    @GetMapping
    public AjaxResult get() {
        return AjaxResult.success(ptAppUserService.get(String.valueOf(getUserId())));
    }

    @ApiOperation("数据列表")
    @RequestMapping(value = "/list")
    public Page list(int page, int rows) {
        PageHelper.startPage(page, rows);
        PageHelper.orderBy("USER_REGDATE desc"); // 根据注册时间倒序排列
        List<PtAppUser> queryResult = ptAppUserService.selectByPrimaryKey("");

        // 取分页信息
        PageInfo<PtAppUser> pageInfo = new PageInfo<>(queryResult);
        return PageUtils.convert(pageInfo);
    }


    @ApiOperation("根据主键获取数据")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public AjaxResult get(String key) {
        List<PtAppUser> ptAppUser = ptAppUserService.selectByPrimaryKey(key);
        if (ptAppUser.get(0) == null) {
            return AjaxResult.error("未找到相关数据！");
        } else {
            return AjaxResult.success(ptAppUser.get(0));
        }
    }


    @ApiOperation("去编辑页面")
    @RequestMapping(value = "/to_edit")
    public ModelAndView toEdit(String key) {
        List<PtAppUser> ptAppUser = ptAppUserService.selectByPrimaryKey(key);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("ptAppUser", ptAppUser.get(0));
        modelAndView.setViewName("mng/pt_app_user_edit");
        return modelAndView;
    }


    @ApiOperation("根据主键更新数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public AjaxResult update(PtAppUser ptAppUser) {
        int ret = ptAppUserService.updateByPrimaryKey(ptAppUser);
        return ret > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @ApiOperation("根据主键删除数据")
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public AjaxResult delete(String userId) {
        String[] idArr = userId.split(",");
        for (String idStr : idArr) {
            ptAppUserService.deleteByPrimaryKey(idStr);
        }
        return AjaxResult.success();
    }

    @RequestMapping(value = "/check/name", method = RequestMethod.POST)
    public Boolean checkName(String userName) {
        List<PtAppUser> ptAppUser = ptAppUserService.selectByName(userName);
        if (ptAppUser.size() > 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 修改操作：根据管理员和企业用户做不同的修改操作
     * <p>
     * 分两个模块：basicdata == 基本资料；pwd == 安全设置
     * 每个模块分两种用户：企业用户和管理员用户
     *
     * @Date 2019/8/15
     */
//    @RequestMapping(value = "/edit", method = RequestMethod.POST)
//    public AjaxResult edit(String customerType, PtAppUser ptAppUser, SysUser sysUser, String flag, String userPassOld, String userPassNew) {
//        /**
//         * 第一步：区分模块
//         *
//         * 第二步：区分用户
//         *
//         * 第三步: 重新给secritysession对象赋值
//         *
//         */
//        int ret = 0;
////        UserProfile userProfile = (UserProfile) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//
//        if ("basicdata".equals(flag)) {
//            if ("企业用户".equals(customerType)) {
//                ret = ptAppUserService.updateByPrimaryKey(ptAppUser);
//            } else if ("管理员".equals(customerType)) {
//                sysUser.setMail(ptAppUser.getUserEmail());
//                sysUser.setMobile(ptAppUser.getUserPhone());
//                ret = sysUserMapper.updateByPrimaryKeySelective(sysUser);
//            }
//
//            // 重新给secritysession对象赋值
////            userProfile.setEmail(ptAppUser.getUserEmail());
////            userProfile.setTel(ptAppUser.getUserPhone());
//        } else if ("pwd".equals(flag)) {
//            String pwdOld = MD5Util.encode(userPassOld);
//            String pwdNew = MD5Util.encode(userPassNew);
//
//            // 先判断当前密码是否正确
////            String pwdNow = userProfile.getPassword();
////            if (pwdOld.equals(pwdNow)) {
//            if ("企业用户".equals(customerType)) {
//                ptAppUser.setUserPass(pwdNew);
//                ret = ptAppUserService.updateByPrimaryKey(ptAppUser);
//            } else if ("管理员".equals(customerType)) {
//                sysUser.setPass(pwdNew);
//                ret = sysUserMapper.updateByPrimaryKeySelective(sysUser);
//            }
//
//        }
//        return ret > 0 ? AjaxResult.success() : AjaxResult.error();
//    }
//

}
