package com.dgtis.dorm.manager.wx.web;

import com.dgtis.dorm.manager.db.domain.AlumniOrganizeUser;
import com.dgtis.dorm.manager.db.domain.AlumniOrganizeUserCustom;
import com.dgtis.dorm.manager.db.service.AlumniOrganizeUserService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import com.dgtis.dorm.manager.wx.util.PinYinUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.*;


/**
 *
 * 组织用户
 *
 * @author ling
 */
@RestController
@RequestMapping("/wx/organizeuser")
public class WxOrganizeUserController {

    @Autowired
    private AlumniOrganizeUserService alumniOrganizeUserService;


    /*********************************** 通用 *******************************************/
    /**
     *
     * 加入专委会
     *
     * @return
     */
    @RequestMapping("addOrganizeUser")
    public Object addOrganizePlace(Integer userId,Integer orgId,String formId,String memo){

        if (userId == null && orgId == null){
            return ResponseUtil.fail("申请加入失败！");
        }

        int r = alumniOrganizeUserService.addOrganize(userId,orgId,memo,formId);

        if (r <= 0){
            return ResponseUtil.fail("申请加入失败！");
        }

        return ResponseUtil.ok(r);
    }


    /**
     * 退出委员会
     *
     * @return
     */
    @RequestMapping("quitOrganizeUser")
    public Object quitOrganizePlace(Integer orgUserId,String delMemo){

        if (orgUserId == null){
            return ResponseUtil.fail("操作失败！");
        }

        int r = alumniOrganizeUserService.delOrganize(orgUserId,delMemo);

        if (r <= 0){
            return ResponseUtil.fail("操作失败！");
        }

        return ResponseUtil.ok(r);
    }

    /**
     * 查询委员会成员
     *
     * @return
     */
    @RequestMapping("selectOrganizeUser")
    public Object selectOrganizePlaceUser(Integer orgId, String userName, Integer userId, @RequestParam(value = "exclusiveOrgUserId",defaultValue = "") ArrayList<Integer> exclusiveOrgUserId, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                          @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        if (orgId == null){
            return ResponseUtil.fail("orgId不能为空");
        }

        Map<String,Object> map = alumniOrganizeUserService.selectByOrgIdAndUse(orgId,userName,userId,exclusiveOrgUserId,page,limit);


        Map<String,List<AlumniOrganizeUserCustom>> ouMap = new HashMap<>(16);

        List<AlumniOrganizeUserCustom> list = (List<AlumniOrganizeUserCustom>) map.get("items");

        for (AlumniOrganizeUserCustom alumniOrganizeUserCustom : list){

            // 获取拼音
            String py = PinYinUtil.cn2Spell(alumniOrganizeUserCustom.getUserName());

            // 获取拼音首字母
            String pyFirst  = py.substring(0,1).toUpperCase();

            char charPyFirst = pyFirst.toCharArray()[0];

            if (charPyFirst < 65 || charPyFirst > 90){
                pyFirst = "#";
            }

            if (ouMap.containsKey(pyFirst)){
                ouMap.get(pyFirst).add(alumniOrganizeUserCustom);
            } else {
                List<AlumniOrganizeUserCustom> ouList = new ArrayList<>();
                ouList.add(alumniOrganizeUserCustom);
                ouMap.put(pyFirst,ouList);
            }
        }

        // 排序
        Map<String, List<AlumniOrganizeUserCustom>> sortMap = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String obj1, String obj2) {
                if (obj1.toCharArray()[0] < 65){
                    obj1 = String.valueOf((char)(obj1.toCharArray()[0] + 60));
                }

                if (obj2.toCharArray()[0] < 65){
                    obj2 = String.valueOf((char)(obj2.toCharArray()[0] + 60));
                }
                return obj1.compareTo(obj2);
            }
        });

        sortMap.putAll(ouMap);

        map.put("items",sortMap);

        return ResponseUtil.ok(map);
    }

    /**
     * 设置专委会管理员
     *
     * @param orgId 组织id
     * @param userId 登录用户id;
     * @param orgUserId 组织用户id
     *
     * @return
     */
    @RequestMapping("addOrganizeAdminn")
    public Object addOrganizeAdminn(Integer orgId,Integer userId,String orgUserId){

        AlumniOrganizeUserCustom user = alumniOrganizeUserService.selectOrgUserByOrgIdAndUserId(orgId,userId);

        if (user == null || "3".equals(user.getPriType()) || "2".equals(user.getPriType())) {
            return ResponseUtil.fail("您不是群主!");
        }

        int f = alumniOrganizeUserService.updatePriTypeByPrimaryKeySelectiveBatch(Arrays.asList(orgUserId.split(",")),userId,orgId);

        if (f > 0){
            return ResponseUtil.ok(f);
        } else {
            return ResponseUtil.fail("操作失败！");
        }

    }




    /**
     *  踢人
     *
     * @param orgId 组织id
     * @param orgUserId 被踢人orgUserId
     * @param adminUserId 踢人用户id
     * @return
     */
    @RequestMapping("rejectOrganizeUserByUserId")
    public Object rejectOrganizeUserByUserId(Integer orgId,String orgUserId,Integer adminUserId){

        try {
            AlumniOrganizeUserCustom user = alumniOrganizeUserService.selectOrgUserByOrgIdAndUserId(orgId,adminUserId);

            if (user == null || "3".equals(user.getPriType())) {
                return ResponseUtil.fail("您不是群主或管理员!");
            }

            int f = alumniOrganizeUserService.updateDeleteByPrimaryKeySelectiveBatch(Arrays.asList(orgUserId.split(",")),adminUserId,orgId);

            if (f > 0){
                return ResponseUtil.ok(f);
            } else {
                return ResponseUtil.fail("操作失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail("操作失败！");
        }
    }






    /*********************************** 地方专委会 *******************************************/







    /*********************************** 行业专委会 *******************************************/






    /*********************************** 兴趣小组 *********************************************/


    /**
     * 获取未审核组织成员
     *
     * @param orgId 组织id
     * @param userId 管理员用户id
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("selectNoNormalOrgUser")
    public Object selectNoNormalOrgUser(Integer orgId,Integer userId,@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        Map<String,Object> map = alumniOrganizeUserService.selectOrganizeNoNormalByUserIdAndOrgId(userId,orgId,page,limit);

        return ResponseUtil.ok(map);
    }


    /**
     *
     * 审核人员
     *
     * @param orgUserId 组织id
     * @param auditUserId 组织用户id
     * @return
     */
    @RequestMapping("auditOrganizeUser")
    public Object auditOrganizeUser(Integer orgId,Integer orgUserId,Integer auditUserId,String normal,String auth_memo){

        AlumniOrganizeUserCustom user = alumniOrganizeUserService.selectOrgUserByOrgIdAndUserId(orgId,auditUserId);

        if (user == null || "3".equals(user.getPriType())) {
            return ResponseUtil.fail("您不是群主或管理员!");
        }

        AlumniOrganizeUser alumniOrganizeUser = new AlumniOrganizeUser();
        alumniOrganizeUser.setId(orgUserId);
        alumniOrganizeUser.setIsNormal(normal);
        alumniOrganizeUser.setFkOrganizeId(orgId);
        alumniOrganizeUser.setAuthUserId(auditUserId);
        alumniOrganizeUser.setAuthDate(LocalDateTime.now());
        if (!"".equals(auth_memo) && auth_memo!=null) {
            alumniOrganizeUser.setAuthMemo(auth_memo);
        }
        alumniOrganizeUser.setIsDelete(true);
        alumniOrganizeUser.setModifyUserId(auditUserId);
        alumniOrganizeUser.setModifyDate(LocalDateTime.now());

        int f =  alumniOrganizeUserService.auditOrganizeUser(alumniOrganizeUser);

        if (f >= 1){
            return ResponseUtil.ok(f);
        }else {
            return ResponseUtil.fail("审核失败!");
        }

    }
}
