package io.renren.modules.admin.controller;

import java.util.Map;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.admin.entity.MemberEntity;
import io.renren.modules.admin.service.MemberService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.admin.service.AttendersService;
import io.renren.modules.admin.service.EmployeeService;
import io.renren.modules.admin.entity.EmployeeEntity;

/**
 * 会员表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-01-17 11:29:03
 */
@RestController
@RequestMapping("/admin/member")
@Api("会员管理")
public class MemberController extends AbstractController  {
    @Autowired
    private MemberService memberService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private AttendersService attendersService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:member:list")
    @ApiOperation("列表")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);
                
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:member:info")
    @ApiOperation("个人信息")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        List<Map<String, Object>> meetingList = attendersService.getMeetingListByMemberId(id);
        Long servicerId = member.getServicerId();
        EmployeeEntity employee = new EmployeeEntity();
        if (servicerId != 0) {
            employee = employeeService.getById(servicerId);
        }
        
        return R.ok().put("member", member).put("meetingList", meetingList).put("employee", employee);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:member:save")
    @ApiOperation("新增")
    public R save(@RequestBody MemberEntity member){
        ValidatorUtils.validateEntity(member, AddGroup.class);
		
		member.setCreateId(getUserId());
		memberService.saveMember(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:member:update")
    @ApiOperation("修改")
    public R update(@RequestBody MemberEntity member){
        ValidatorUtils.validateEntity(member, UpdateGroup.class);
        member.setModifyTime(new Date());
        member.setModifyId(getUserId());
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:member:delete")
    @ApiOperation("删除")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("/status")
    @RequiresPermissions("admin:member:status")
    @ApiOperation("改变审核状态")
    public R status(@RequestBody Map<String, Object> params){
    //    System.out.println(params);      
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        // System.out.println(id);
        // System.out.println(status);
        memberService.updateStatus(id, status);
        return R.ok();
    }

    @PostMapping("/repassword/{id}")
    @RequiresPermissions("admin:member:repassword")
    @ApiOperation("重置默认密码")
    public R repassword(@PathVariable("id") Long id){
        MemberEntity member = memberService.getById(id);
        member.setPassword("123456");
        member.setModifyTime(new Date());
        member.setModifyId(getUserId());
		memberService.updateById(member);
        return R.ok();
    }
}
