package com.winsun.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.UniversityInfo;
import com.winsun.bean.YsmUser;
import com.winsun.mapper.UniversityInfoMapper;
import com.winsun.mapper.YsmUserMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能平台账号实名管理
 * @Author: chancy
 * @Date:
 */
@Slf4j
@RestController
@RequestMapping("/ysmUser")
public class YsmUserController extends BaseController {

    private static YsmUserMapper ysmUserMapper;

    @Autowired
    public YsmUserController(YsmUserMapper ysmUserMapper) {
        YsmUserController.ysmUserMapper = ysmUserMapper;
    }

    /**
     * 添加实名智能平台账号
     * @param ysmUser 添加对象的json字符串
     * @return
     */
    @Permission(menuname = "添加实名账号", value = "insert", method = RequestMethod.POST)
    public ResponseData<String> insertYsmUser(@RequestParam("ysmUser") String ysmUser) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        YsmUser ysm = JSON.parseObject(ysmUser, YsmUser.class);
        ysmUserMapper.insert(ysm);
        return ResponseData.success("添加成功！");
    }

    /**
     *  不通过佣金系统
     * @param id   不通过佣金系统账号的ID
     * @return
     */
    @Permission(menuname = "不通过佣金系统", value = "notThrough/{id}", method = RequestMethod.POST)
    public ResponseData<String> notThroughYsmUser(@PathVariable("id") Integer id,@RequestParam("status") Integer status) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("status", status);
        ysmUserMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("修改成功！");
    }

    /**
     * 编辑实名智能平台账号
     * @param ysmUser 编辑对象的json字符串
     * @return
     */
    @Permission(menuname = "编辑实名智能平台账号", value = "update", method = RequestMethod.POST)
    public ResponseData<String> updateYsmUser(@RequestParam("ysmUser") String ysmUser) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        YsmUser ysm = JSON.parseObject(ysmUser, YsmUser.class);
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id",ysm.getId());
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("user_name", ysm.getUserName());
        dataMapping.put("sign", ysm.getSign());
        ysmUserMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("修改成功！");
    }

    /**
     *查询实名智能平台账号
     * @param userName  实名账号
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询实名智能平台账号", value = "list", method = RequestMethod.POST)
    public ResponseData<Page<YsmUser>> listYsmUser(@RequestParam("userName") String userName,
                                                         @RequestParam(name = "pageNo") int pageIndex, @RequestParam(name = "pageSize") int pageSize) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Wrapper<YsmUser> wrapper = new EntityWrapper();
        wrapper.like(StringUtils.isNotBlank(userName), "user_name", userName, SqlLike.DEFAULT);
        Page<YsmUser> page = new Page<>(pageIndex, pageSize);
        List<YsmUser> ysmUsers = ysmUserMapper.selectPage(page, wrapper);
        page.setRecords(ysmUsers);
        return ResponseData.success(page, "查询成功！");
    }

    @Permission(menuname = "查询", value = "getlist", method = RequestMethod.POST)
    public ResponseData<List<Map<String,Object>>> getlistYsmUser() {
        Wrapper<YsmUser> wrapper = new EntityWrapper();
        wrapper.setSqlSelect("id,user_name as userName");
        return ResponseData.success(ysmUserMapper.selectMaps(wrapper), "查询成功！");
    }
}
