package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
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.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 org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

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

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

    //新用户选择套餐页面*
    @RequestMapping(value = "/chooseArea")
    @ResponseBody
    public String chooseArea(String partner, RedirectAttributes attr) {
        List<Map<String, Object>> areaQrCode = new ArrayList<>();
        if(StringUtils.isNotBlank(partner)){
            //areaQrCode =hhrManager.findAreaQrCode(partner);
        }else{
            return "redirect:/customer/nocard.do";
        }

        if(areaQrCode.size()!=1){
            return "redirect:/customer/nocard.do";
        }
        return "mobilePhone/yrym_chooseArea";
    }
    /**
     *
     */
    @RequestMapping(value = "/findAreaCustomer")
    @ResponseBody
    public ResponseData<Map<String,Object>> findAreaCustomer(@RequestParam("id")Integer id) {

        Map<String,Object> objMaps = new HashMap<>();
        UserDeploy userDeploy = userDeployMapper.selectById(id);
        List<String> schoolNameList = new ArrayList<>();
        if(userDeploy != null){
            userDeploy.setSubNames(userDeploy.getSubNames().substring(0,userDeploy.getSubNames().length()-1));
            userDeploy.setSchoolNames(userDeploy.getSchoolNames().substring(0,userDeploy.getSchoolNames().length()-1));
            userDeploy.setHhrIds(userDeploy.getHhrIds().substring(0,userDeploy.getHhrIds().length()-1));
            userDeploy.setHhrNames(userDeploy.getHhrNames().substring(0,userDeploy.getHhrNames().length()-1));
            Map<String,String> subMap = new HashMap<>();
            String[] subNames = userDeploy.getSubNames().split(",");
            for (String subName : subNames) {
                subMap.put(subName,subName);
            }
            String[] schoolNames = userDeploy.getSchoolNames().split(",");
            for (int i=0 ; i < schoolNames.length;i++){
                schoolNameList.add(schoolNames[i]);
            }
            Wrapper<School> schoolWrapper = new EntityWrapper<>();
            schoolWrapper.in("school_name",schoolNameList);
            List<School> schoolList = schoolMapper.selectList(schoolWrapper);

            for (String s : subMap.keySet()) {
                List<School> school1s = new ArrayList<>();
                for (School school : schoolList) {
                    if(school.getSubName().equals(s)){
                        school1s.add(school);
                    }
                }
                objMaps.put(s,school1s);
            }
            objMaps.put("subMap",subMap);
            objMaps.put("userDeploy",userDeploy);
            return ResponseData.success(objMaps);
        }
        return ResponseData.error("没有查询相关的合伙人配置信息！！！");
    }

    /**
     * 初始合伙人列表
     * @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());
        }
        Wrapper<SysUser> sysUserWrapper = new EntityWrapper<>();
        sysUserWrapper.setSqlSelect("id,name");
        sysUserWrapper.in("id",list);
        List<SysUser> sysUserList = sysUserMapper.selectList(sysUserWrapper);
        return ResponseData.success(sysUserList);
    }
    /**
     * 初始合伙人列表
     * @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);
    }
}
