package com.kiisoo.ic.employee.controller;

import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Description: 用户管理controller
 * @Auther: yechenhao
 * @Date: 2020/4/7 0002 10:06
 * @Version: v1
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class EmployeeController extends BaseController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 新增成员
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listUser(){
        try {
            List<PrivilageCpUserDO> privilageCpUserDOS = employeeService.listUser();
            return data(privilageCpUserDOS);
        }catch (Exception e){
            log.error("添加用户失败",e);
            return fail();
        }
    }
    /**
     * 新增成员
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "img/download",method = RequestMethod.POST)
    public void listUser(@RequestParam("url")String url,HttpServletResponse response){
        BufferedInputStream in = null;
        try {

            in = new BufferedInputStream(employeeService.getInputStream(url));
            //输入到response
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] bufferNew = new byte[1024];
            int lenNew;
            while ((lenNew = in.read(bufferNew)) > 0) {
                outputStream.write(bufferNew, 0, lenNew);
            }
            outputStream.flush();
        }catch (Exception e){
            log.error("添加用户失败",e);
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 新增成员
     * @param response 请求响应
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "qrCode",method = RequestMethod.GET)
    public void downLoadQrCode(HttpServletResponse response){
        try {
            employeeService.downLoadQrCode(response);
        }catch (Exception e){
            log.error("下载二维码",e);
        }
    }

    /**
     * 同步成员
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "sync",method = RequestMethod.GET)
    @ResponseBody
    public Map<String,Object> syncUser(){
        try {
            employeeService.syncUser();
            return data(null);
        }catch (Exception e){
            log.error("添加用户失败",e);
            return fail();
        }
    }

    /**
     * 新增成员
     * @param employee 成员实体
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> addUser(EmployeeDO employee){
        try {
            //标识是否同步发邮件和短信
            if (null != employee.getCheckbox() && employee.getCheckbox().length >0){
                employee.setToInvite(true);
            }
            String code = employeeService.addUser(employee);
            return data(code);
        }catch (Exception e){
            log.error("添加用户失败",e);
            return fail();
        }
    }

    /**
     * 修改成员
     * @param employee 成员实体
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> updateUser(@RequestBody EmployeeDO employee){
        try {
            Boolean hasAdd = employeeService.updateUser(employee);
            return data(hasAdd);
        }catch (Exception e){
            log.error("修改用户失败",e);
            return fail();
        }
    }

    /**
     * 删除成员
     * @param cpUserId 成员id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> deleteUser(@RequestParam("cpUserId") Long cpUserId){
        try {
            Boolean hasAdd = employeeService.deleteUser(cpUserId);
            return data(hasAdd);
        }catch (Exception e){
            log.error("删除用户失败",e);
            return fail();
        }
    }
}
