package com.zd.app.controller;

import com.zd.common.core.constant.SecurityConstants;
import com.zd.common.core.domain.R;
import com.zd.common.core.domain.qc.Base64DecodeMultipartFile;
import com.zd.common.core.utils.file.ImageUtils;
import com.zd.common.security.service.TokenService;
import com.zd.system.api.RemoteDeptService;
import com.zd.system.api.RemoteUserService;
import com.zd.system.api.model.LoginUser;
import com.zd.system.api.model.SimpleUserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.net.util.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Controller
 *
 * @author cyl
 * @date 2021/9/17
 */
@RestController
@RequestMapping("/sysuser/api")
@Api(tags = {"用户相关"})
public class SysUserApi {

    @Autowired
    private TokenService tokenService;

    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private RemoteDeptService remoteDeptService;

    @DeleteMapping("/logout")
    public R<?> logout(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (com.zd.common.core.utils.StringUtils.isNotNull(loginUser)) {
            // 删除用户缓存记录
            tokenService.delLoginUser(loginUser.getToken());
        }
        return R.ok();
    }

    @ApiOperation(value = "查询当前用户简略信息")
    @GetMapping(value = "/my/simple/info")
    public R<SimpleUserVO> myInfoSimple() {
        R<SimpleUserVO> simpleUserVOR = remoteUserService.myInfoSimple(SecurityConstants.INNER);
        return simpleUserVOR;
    }



    @ApiOperation(value = "小程序查询院系列表（用户端，管理端）")
    @GetMapping(value = "/departments/list")
    public R<Object> departmentsList() {
        return remoteUserService.departmentsList();
    }

    @ApiOperation(value = "小程序查询院系下楼栋列表（用户端，管理端）")
    @GetMapping(value = "/building/list")
    public R<Object> departmentsList(@RequestParam Long deptId) {
        return remoteUserService.building(deptId);
    }

    /**
     * 小程序端 电子签名抠图生成图片
     * @return base64 图片
     */
    @PostMapping("/genSign")
    @ApiOperation(value = "小程序电子签名抠图生成图片")
    public R<String> getPsPictureSign(@RequestParam("file") MultipartFile file){
        try {
            // 抠图工具类 169为灰色
            return R.ok(ImageUtils.transparent(file.getInputStream(), 152));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.fail("生成失败！");

    }

    @PostMapping("/querySignature")
    @ApiOperation(value = "小程序用户查询签名")
    public R querySignature(){
        return remoteUserService.querySignature();
    }

    @PostMapping("/updateSignature")
    @ApiOperation(value = "小程序用户修改签名")
    public R updateSignature(@RequestBody Map<String, String> params){
        String signature = params.get("signature");

        byte[] b = Base64.decodeBase64(signature);
        for (int i = 0; i < b.length; ++i) {
            if (b[i] < 0) {
                b[i] += 256;
            }
        }

        String header = "data:image/jpeg;base64,"+signature;
        MultipartFile file = new Base64DecodeMultipartFile(b, header);
        remoteUserService.updateSignature(file);
        return R.ok();
    }


    @ApiOperation(value = "用户查询是否有电子签章")
    @GetMapping("/queryDeptSignByUserId")
    public R<Map<String, Object>> queryDeptSignByUserId() {
        Map<String, Object> map = remoteDeptService.queryDeptSignByUserId();
        return R.ok(map);
    }
}
