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

import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.db.domain.AlumniOrganize;
import com.dgtis.dorm.manager.db.domain.AlumniOrganizeUser;
import com.dgtis.dorm.manager.db.domain.AlumniOrganizeUserCustom;
import com.dgtis.dorm.manager.db.domain.AlumniUser;
import com.dgtis.dorm.manager.db.service.AlumniOrganizeService;
import com.dgtis.dorm.manager.db.service.AlumniOrganizeUserService;
import com.dgtis.dorm.manager.db.service.UserService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import com.dgtis.dorm.manager.db.util.wx.entity.MessageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
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.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/organizeuser")
public class OrganizeUserController {

    @Autowired
    private AlumniOrganizeUserService alumniOrganizeUserService;

    @Autowired
    private AlumniOrganizeService alumniOrganizeService;

    @Autowired
    private UserService userService;

    /**
     * 根据orgId查询组织用户
     *
     * @param fkOrganizeId 组织id
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("selectOrganizeUserByOrgId")
    public Object selectOrganizeUserByOrgId(@LoginAdmin Integer adminId, Integer fkOrganizeId, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        Map<String,Object> map = alumniOrganizeUserService.selectByOrgId(fkOrganizeId,page,limit);

        return ResponseUtil.ok(map);
    }


    /**
     * 根据orgId查询组织正常用户
     *
     * @param fkOrganizeId 组织id
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("selectOrganizeUserByOrgIdNoDel")
    public Object selectOrganizeUserByOrgIdNoDel(@LoginAdmin Integer adminId, Integer fkOrganizeId, @RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        Map<String,Object> map = alumniOrganizeUserService.selectByOrgIdNoDel(fkOrganizeId,page,limit);

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

        for (AlumniOrganizeUserCustom alumniOrganizeUserCustom : list){
            if (alumniOrganizeUserCustom.getPriType().equals("1")){
                map.put("adminUser", alumniOrganizeUserCustom != null?alumniOrganizeUserCustom.getId():"");
                break;
            }
        }

        return ResponseUtil.ok(map);
    }

    @PostMapping("updateOrganizeUserById")
    public Object updateOrganizeUserById(@LoginAdmin Integer adminId,AlumniOrganizeUserCustom alumniOrganizeUserCustom){

        alumniOrganizeUserCustom.setModifyDate(LocalDateTime.now());
        alumniOrganizeUserCustom.setModifyUserId(adminId);

        // isDelete为false时添加删除时间
        if (alumniOrganizeUserCustom.getIsDelete() != null && !alumniOrganizeUserCustom.getIsDelete()){
            alumniOrganizeUserCustom.setDeleteDate(LocalDateTime.now());
        }

        Integer f = alumniOrganizeUserService.updateByPrimaryKeySelective(alumniOrganizeUserCustom);

        return ResponseUtil.ok(f);
    }

    @PostMapping("sendWXMsg")
    public Object sendWXMsg(Integer orgUserId){

        AlumniOrganizeUser alumniOrganizeUser = alumniOrganizeUserService.selectByPrimaryKey(orgUserId);

        AlumniOrganize alumniOrganize = alumniOrganizeService.selectByPrimaryKey(alumniOrganizeUser.getFkOrganizeId());

        // 发送微信消息
        AlumniUser u  = userService.findById(alumniOrganizeUser.getFkUserId());
        MessageData msgData = new MessageData();
        msgData.setKeyword1(u.getUserName());
        msgData.setKeyword2(alumniOrganize.getOrganizeName()+"专委会申请");
        if ("1".equals(alumniOrganizeUser.getIsNormal())){
            msgData.setKeyword3("通过");
            msgData.setKeyword4("您申请的"+ alumniOrganize.getOrganizeName()+"审核通过!");
        } else if ("2".equals(alumniOrganizeUser.getIsNormal())){
            msgData.setKeyword3("驳回");
            msgData.setKeyword4("您申请的"+ alumniOrganize.getOrganizeName()+"审核不通过!");
        }

        userService.sendWXMSG(alumniOrganizeUser.getFkUserId(),msgData);

        return ResponseUtil.ok();

    }
}
