package com.winsun.controller.school;

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.bean.School;
import com.winsun.bean.SysUser;
import com.winsun.mapper.SchoolMapper;
import com.winsun.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: chancy
 * @Date: 2020/2/12 17:25 学校管理
 */
@Slf4j
@RestController
@RequestMapping("/school/schoolAllocation")
public class SchoolAllocationController extends BaseController {

	private static SchoolMapper schoolMapper;
    private static SysUserMapper sysUserMapper;

    @Autowired
    public SchoolAllocationController(SchoolMapper schoolMapper, SysUserMapper sysUserMapper) {
        SchoolAllocationController.schoolMapper = schoolMapper;
        SchoolAllocationController.sysUserMapper = sysUserMapper;
    }

    @Permission(menuname = "添加学校分配信息", value = "addSchoolAllocation", method = RequestMethod.POST)
    public ResponseData<String> insertSchool(
            @RequestParam("account") String account,
            @RequestParam("schoolId") String schoolId) {

        if(StringUtils.isBlank(account) || "undefined".equals(account)){
            return ResponseData.error("账号不能为空！！!");
        }
        List<String> list = Arrays.asList(account.split(","));
        for (String acc : list) {
            Wrapper<SysUser> wrapperUser = new EntityWrapper<SysUser>();
            wrapperUser.eq("account", acc);
            //wrapperUser.eq(column, params);
            List<SysUser> userList = sysUserMapper.selectList(wrapperUser);
            if (userList.size() > 0){
                schoolMapper.addSupervisorSchool(userList.get(0).getId(), schoolId);
                return ResponseData.success("添加成功！");
            }
        }
        return ResponseData.error("账号不存在！！");
    }

    @Permission(menuname = "删除学校分配信息", value = "deleteSchoolAllocation", method = RequestMethod.POST)
    public ResponseData<String> deleteSchool(@RequestParam("id") String id) {

        int num = schoolMapper.deleteSupervisorSchool(id);

        if (num == 1){
            return ResponseData.success("删除成功！");
        }
        return  ResponseData.error("删除失败");

    }

    /**
     *
     * @param account
     * @param name
     * @param substName
     * @param schoolName
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询学校分配", value = "findByList", method = RequestMethod.POST)
    public ResponseData<Page<Map<String,Object>>> listSchool(
            @RequestParam("account") String account, @RequestParam("name") String name,
            @RequestParam("substName") String substName, @RequestParam("schoolName") String schoolName,
            @RequestParam(name = "pageNo", required = false) int pageIndex,
            @RequestParam(name = "pageSize", required = false) int pageSize) {

        List<Map<String, Object>> dataList = sysUserMapper.schoolAllocationList(account,name,substName,schoolName);
        Page<Map<String, Object>> page = new Page<>(pageIndex, pageSize);
        List<Map<String, Object>> newList = new ArrayList<Map<String,Object>>();
        if(pageIndex*pageSize > dataList.size()) {
        	newList = dataList.subList((pageIndex-1)*pageSize, dataList.size());
        }else {
        	newList = dataList.subList((pageIndex-1)*pageSize, pageIndex*pageSize);
        }

        page.setRecords(newList);
        page.setTotal(dataList.size());
        return ResponseData.success(page, "查询完毕！");
    }



}
