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.School;
import com.winsun.bean.SysUser;
import com.winsun.bean.UserDeploy;
import com.winsun.bean.UserSchool;
import com.winsun.mapper.SchoolMapper;
import com.winsun.mapper.SysUserMapper;
import com.winsun.mapper.UserDeployMapper;
import com.winsun.mapper.UserSchoolMapper;
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.util.ArrayList;
import java.util.List;

/**
 * 〈合伙人配置信息管理〉
 *
 * @author PXL
 * @create 2020/6/8 11:53
 */
@Slf4j
@RestController
@RequestMapping("/userDeploy")
public class UserDeployController extends BaseController {

    @Autowired
    private UserDeployMapper userDeployMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SchoolMapper schoolMapper;
    @Autowired
    private UserSchoolMapper userSchoolMapper;

    /**
     * 合伙人配置信息编辑
     *
     * @param userDeploy 编辑对象json字符串
     * @return
     */
    @Permission(menuname = "修改或者添加合伙人配置信息", value = "addOrUpdate", method = RequestMethod.POST)
    public ResponseData<String> updateProduct(@RequestParam("userDeploy") String userDeploy) {
        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        UserDeploy pro = JSON.parseObject(userDeploy, UserDeploy.class);
        Integer obj = pro.getId();
        if(obj != null){
            Wrapper wrapper = new EntityWrapper<>();
            wrapper.eq("id", pro.getId());
            userDeployMapper.update(pro,wrapper);
            return ResponseData.success("修改成功！");
        }
        userDeployMapper.insert(pro);
        return ResponseData.success("新增成功！");
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public ResponseData<String> delete(Integer id) {
        try {
            userDeployMapper.deleteById(id);
        } catch (Exception e) {
            log.info(e.getMessage());
            return ResponseData.error("删除失败！！！");
        }
        return ResponseData.success("删除成功！！！");
    }

    /**
     * 合伙人配置信息查詢
     * @param name
     * @param hhrNames
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询合伙人配置信息", value = "list", method = RequestMethod.POST)
    public ResponseData<Page<UserDeploy>> listProduct(
            @RequestParam(name = "name", required = false) String name, @RequestParam(name = "hhrNames", required = false) String hhrNames,
            @RequestParam(name = "pageNo") int pageNo, @RequestParam(name = "pageSize") int pageSize) {

        ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }
        Wrapper<UserDeploy> wrapper = new EntityWrapper();
        wrapper.like(StringUtils.isNotBlank(name), "name", name, SqlLike.DEFAULT);
        wrapper.like(StringUtils.isNotBlank(hhrNames), "hhrNames", hhrNames, SqlLike.DEFAULT);
        Page<UserDeploy> page = new Page<>(pageNo, pageSize);
        List<UserDeploy> list = userDeployMapper.selectPage(page, wrapper);
        page.setRecords(list);
        return ResponseData.success(page, "查询成功！");
    }

    /**
     * 初始合伙人列表
     * @param schoolId
     * @return
     */
    @Permission(menuname = "获取合伙人列表", value = "getHhrs", method = RequestMethod.POST)
    public ResponseData<List<SysUser>> getHhrs(@RequestParam(name = "schoolId", required = false) String schoolId){
        Wrapper<UserSchool> userSchoolWrapper = new EntityWrapper<>();
        userSchoolWrapper.eq(StringUtils.isNotBlank(schoolId),"school_id",schoolId);
        List<UserSchool> userSchools = userSchoolMapper.selectList(userSchoolWrapper);
        List<String> list = new ArrayList<>();
        for (UserSchool userSchool : userSchools) {
            list.add(userSchool.getUserId());
        }
        if(list.size()>0){
            Wrapper<SysUser> sysUserWrapper = new EntityWrapper<>();
            sysUserWrapper.setSqlSelect("id,name");
            sysUserWrapper.in("id",list);
            List<SysUser> sysUserList = sysUserMapper.selectList(sysUserWrapper);
            return ResponseData.success(sysUserList);
        }
        return ResponseData.success();
    }
    /**
     * 初始合伙人列表
     * @param subName
     * @return
     */
    @Permission(menuname = "获取学校列表", value = "getSchools", method = RequestMethod.POST)
    public ResponseData<List<School>> getSchools(@RequestParam(name = "subName", required = false) String subName){
        Wrapper<School> schoolWrapper = new EntityWrapper<>();
        schoolWrapper.setSqlSelect("id,school_name schoolName");
        schoolWrapper.eq(StringUtils.isNotBlank(subName),"sub_name",subName);
        List<School> list = schoolMapper.selectList(schoolWrapper);
        return ResponseData.success(list);
    }

    /**
     * 解锁账户
     * @param userId
     * @return
     */
    @Permission(menuname = "解锁用户", value =  "unlockUser", method = RequestMethod.POST)
    public ResponseData<String> unlockUser(String userId) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setStatus("1");
        Integer integer = sysUserMapper.updateById(sysUser);

        if (integer > 0) {
            return ResponseData.success("解锁成功");
        }
        return ResponseData.error("解锁失败");
    }

    /**
     * 锁定账户
     * @param userId
     * @returnunlock
     */
    @Permission(menuname = "锁定用户", value =  "lockUser", method = RequestMethod.POST)
    public ResponseData<String> lockUser(String userId) {
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setStatus("2");
        Integer integer = sysUserMapper.updateById(sysUser);
        if (integer > 0) {
            return ResponseData.success("锁定成功");
        }
        return ResponseData.error("锁定失败");
    }
}
