package com.platform.controller;

import com.platform.annotation.CanUserUpdateRecord;
import com.platform.entity.Result;
import com.platform.entity.UserEntity;
import com.platform.service.UserService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;

import com.platform.utils.excel.ExcelExport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Controller
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-16 15:02:28
 */
@Api(tags = "管理后台用户信息接口")
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查看列表
     */
    @ApiOperation(value = "获取用户列表，支持通过微信昵称或者用户名称查询，支持分页")
    @RequestMapping("/list")
    
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名称", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "nickname", value = "用户微信昵称", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "page", value = "当前页码", paramType = "query", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "limit", value = "每页大小", paramType = "query", required = true, dataType = "Long"),
    })
    public Result list(@RequestBody Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<UserEntity> userList = userService.queryList(query);
        int total = userService.queryTotal(query);

        if(!CollectionUtils.isEmpty(userList)){
            for(UserEntity user : userList){
                if(null != user && null != user.getId()){
                    List<String> brandNames = userService.getBindBrands(user.getId());
                    if(CollectionUtils.isEmpty(brandNames)){
                        continue;
                    }
                    user.setBindBrandName(brandNames.stream().filter(name -> name != null).collect(Collectors.joining(",")));
                }
            }
        }

        PageUtils pageUtil = new PageUtils(userList, total, query.getLimit(), query.getPage());

        return Result.success( pageUtil);
    }


    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    
    public Result info(@PathVariable("id") Integer id) {
        UserEntity user = userService.queryObject(id);

        return Result.success(user);
    }

    /**
     * 保存
     */
    @CanUserUpdateRecord
    @RequestMapping("/save")
    
    public Result save(@RequestBody UserEntity user) {
        userService.save(user);

        return Result.success();
    }

    /**
     * 修改
     */
    @CanUserUpdateRecord
    @RequestMapping("/update")
    
    public Result update(@RequestBody UserEntity user) {
        userService.update(user);

        return Result.success();
    }

    /**
     * 删除
     */
    @CanUserUpdateRecord
    @RequestMapping("/delete")
    
    public Result delete(@RequestBody Integer[] ids) {
        userService.deleteBatch(ids);

        return Result.success();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public Result queryAll(@RequestBody Map<String, Object> params) {

        List<UserEntity> userList = userService.queryList(params);

        return Result.success( userList);
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public Result queryTotal(@RequestBody Map<String, Object> params) {
        int sum = userService.queryTotal(params);

        return Result.success(sum);
    }

    /**
     * 导出会员
     */
    @RequestMapping("/export")
//    
    public Result export(@RequestBody Map<String, Object> params, HttpServletResponse response) {

        List<UserEntity> userList = userService.queryList(params);

        ExcelExport ee = new ExcelExport("会员列表");

        String[] header = new String[]{"会员名称", "性别", "会员级别", "手机号码"};

        List<Map<String, Object>> list = new ArrayList<>();

        if (userList != null && userList.size() != 0) {
            for (UserEntity userEntity : userList) {
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                map.put("GENDER", userEntity.getGender() == 1 ? "男" : (userEntity.getGender() == 2 ? "女" : "未知"));
                map.put("LEVEL_NAME", userEntity.getLevelName());
                map.put("USERNAME", userEntity.getUsername());
                map.put("MOBILE", userEntity.getMobile());
                list.add(map);
            }
        }

        ee.addSheetByMap("会员", list, header);
        ee.export(response);
        return Result.success();
    }
}
