package com.winsun.controller.school;

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

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 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;

/**
 * @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) {
    	Wrapper<SysUser> wrapperUser = new EntityWrapper<SysUser>();
    	wrapperUser.eq("account", account);
    	//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) {
       
        Wrapper<SysUser> wrapperUser = new EntityWrapper<SysUser>();
        wrapperUser.like(StringUtils.isNotBlank(name), "name", name, SqlLike.DEFAULT);
        wrapperUser.eq(StringUtils.isNotBlank(account), "account", account);
        List<SysUser> userList = sysUserMapper.selectList(wrapperUser);
        
        Wrapper<School> wrapperSchool = new EntityWrapper<School>();
        wrapperSchool.eq(StringUtils.isNotBlank(schoolName), "school_name", schoolName);
        wrapperSchool.like(StringUtils.isNotBlank(substName), "sub_name", substName, SqlLike.DEFAULT);
        List<School> schoolList = schoolMapper.selectList(wrapperSchool);
        
//        Map<String, Object> schoolMap = MapUtil.listToMap(schoolMapper.getSchoolRef(), "user_id", "school_id");
        List<Map<String, Object>> schoolAllocationList = schoolMapper.getSupervisorRef();
        List<Map<String, Object>> dataList = new ArrayList<Map<String,Object>>();
        
        for(Map<String, Object> schoolAllocation : schoolAllocationList) {
        	String theUserId = Integer.toString((Integer)schoolAllocation.get("user_id"));
        	String theSchoolId = (String)schoolAllocation.get("school");
        	
        	for(SysUser sysUser : userList) {
        		if(sysUser.getId().equals(theUserId)) {
        			Map<String, Object> map = new HashMap<String, Object>();
        			map.put("account", sysUser.getAccount());
        			map.put("name", sysUser.getName());
        			map.put("schoolId", theSchoolId);
        			map.put("userId", sysUser.getId());
        			map.put("id",  schoolAllocation.get("id"));
        			
        			dataList.add(map);
        		}
        	}
        }
        
        for(Map<String, Object> map : dataList) {
        	int theSchoolId = Integer.parseInt((String)map.get("schoolId"));
        	
        	for(School school : schoolList) {
        		if(school.getId() == theSchoolId) {
        			map.put("substName", school.getSubName());
        			map.put("schoolName", school.getSchoolName());
        		}
        	}
        }
        
        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, "查询完毕！");
    }

  

}
