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.AlumniOrganizeCustom;
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.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/organize")
public class OrganizeController {

    private static final Log logger = LogFactory.getLog(OrganizeController.class);


    @Autowired
    private AlumniOrganizeService alumniOrganizeService;

    @Autowired
    private AlumniOrganizeUserService alumniOrganizeUserService;

    @Autowired
    private UserService userService;


    /********************************************************* 通用 *********************************************************/


    /**
     * 根据类型查询组织
     *
     * @param orgType 组织类型
     * @return
     */
    @PostMapping("/selectOrganize")
    public Object selectOrganize(String orgType,String orgName,@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit){

        Map<String,Object> map = alumniOrganizeService.selectOrganizeByTypeAndName(orgType,orgName,page,limit);

        return ResponseUtil.ok(map);
    }

    /**
     * 创建组织
     *
     * @param adminId
     * @param alumniOrganize
     * @return
     */
    @PostMapping("createOrganize")
    public Object createOrganize(@LoginAdmin Integer adminId, @RequestBody AlumniOrganize alumniOrganize){

        alumniOrganize.setCreateDate(LocalDateTime.now());
        alumniOrganize.setOrganizeUserId(adminId);
        //后台创建默认审核通过
        alumniOrganize.setAuthStatus(1);
        alumniOrganize.setAuthUserId(adminId);
        Integer f = alumniOrganizeService.insertSelective(alumniOrganize);


        try {
            // 设置群主
//            Integer ownerUserId = Integer.parseInt(alumniOrganizeCustom.getAdminUserId());
//            alumniOrganizeService.OrganizeAdminUser(alumniOrganizeCustom.getId(),ownerUserId);
        } catch (Exception e) {

        }

        return ResponseUtil.ok(f);
    }

    /**
     * 更新组织
     *
     * @param adminId
     * @param alumniOrganize
     * @return
     */
    @PostMapping("updateOrganize")
    public Object updateOrganize(@LoginAdmin Integer adminId,@RequestBody AlumniOrganize alumniOrganize){

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

        Integer f = alumniOrganizeService.updateByPrimaryKeySelective(alumniOrganize);


        // 设置群主
        // alumniOrganizeService.updateOrganizeAdminUser(alumniOrganizeCustom.getId(),Integer.parseInt(alumniOrganizeCustom.getAdminUserId()));

//        try {
//            Integer ownerUserId = Integer.parseInt(alumniOrganizeCustom.getAdminUserId());
//            alumniOrganizeService.OrganizeAdminUser(alumniOrganizeCustom.getId(),ownerUserId);
//        } catch (Exception e) {
//        }


        return ResponseUtil.ok(f);
    }



    /**
     * 更新组织
     *
     * @param adminId
     * @param alumniOrganizeCustom
     * @return
     */
    @PostMapping("auditOrganizeInterest")
    public Object auditOrganizeInterest(@LoginAdmin Integer adminId,AlumniOrganizeCustom alumniOrganizeCustom){

        alumniOrganizeCustom.setModifyUserId(adminId);
        alumniOrganizeCustom.setModifyDate(LocalDateTime.now());
        alumniOrganizeCustom.setAuthDate(LocalDateTime.now());
        alumniOrganizeCustom.setAuthUserId(adminId);

        Integer f = alumniOrganizeService.updateByPrimaryKeySelective(alumniOrganizeCustom);


        return ResponseUtil.ok(f);
    }

    /**
     * 删除组织
     * @param adminId
     * @param alumniOrganizeCustom
     * @return
     */
    @PostMapping("deleteOrgById")
    public Object deleteOrgById(@LoginAdmin Integer adminId, @RequestBody AlumniOrganizeCustom alumniOrganizeCustom){
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        alumniOrganizeService.deleteOrgById(alumniOrganizeCustom.getId());
        return ResponseUtil.ok();
    }

    /**
     * 更新组织审核状态
     * @param adminId
     * @param alumniOrganize
     * @return
     */
    @RequestMapping(value = "updateOrgAuth")
    public Object updateOrgAuth(@LoginAdmin Integer adminId, @RequestBody AlumniOrganize alumniOrganize){
        if(adminId == null) {
            return ResponseUtil.unlogin();
        }
        alumniOrganizeService.updateByPrimaryKeySelective(alumniOrganize);

        return ResponseUtil.ok();
    }

    @PostMapping("sendWXMsg")
    public Object sendWXMsg(Integer id){
        AlumniOrganize alumniOrganize = alumniOrganizeService.selectByPrimaryKey(id);

        // 发送微信消息
        AlumniUser u  = userService.findById(alumniOrganize.getOrganizeUserId());
        MessageData msgData = new MessageData();
        msgData.setKeyword1(u.getUserName());
        msgData.setKeyword2(alumniOrganize.getOrganizeName()+"兴趣小组申请");
        if ("1".equals(alumniOrganize.getAuthStatus().toString())){
            msgData.setKeyword3("通过");
            msgData.setKeyword4("您的兴趣小组"+ alumniOrganize.getOrganizeName()+"创建申请审核通过!");
        } else if ("2".equals(alumniOrganize.getAuthStatus().toString())){
            msgData.setKeyword3("驳回");
            msgData.setKeyword4("您的兴趣小组"+ alumniOrganize.getOrganizeName()+"创建申请被驳回!");
        }
        userService.sendWXMSG(alumniOrganize.getOrganizeUserId(),msgData);

        return ResponseUtil.ok();
    }

    /**
     * 批量删除圈子
     * @param adminId
     * @param orgIds
     * @return
     */
    @PostMapping("deleteOrgQuantity")
    public Object deleteOrgQuantity(@LoginAdmin Integer adminId,String orgIds){
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        if (orgIds == null) {
            return ResponseUtil.badArgument();
        }
        alumniOrganizeService.deleteOrgQuantity(Arrays.asList(orgIds.split(",")));
        return ResponseUtil.ok();
    }


    @PostMapping("selectUserByUserName")
    public Object selectUserByUserName(@LoginAdmin Integer adminId,String userName){

        List<AlumniUser> list = userService.selectUserByUserName(userName);

        return ResponseUtil.ok(list);
    }

    /**
     * 方法描述  查询详情
     * @param id  组织ID
     * @return Object
     *
     * @author huanghaoqi
     * @date 2019年01月07日 14:27:59
     */
    @PostMapping("detail")
    public Object detail(@LoginAdmin Integer adminId,Integer id){
        AlumniOrganizeCustom alumniOrganizeCustom = alumniOrganizeService.selectById(id);
        return ResponseUtil.ok(alumniOrganizeCustom);
    }






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


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


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