package com.winsun.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.QRCode;
import com.winsun.mapper.QRCodeMapper;
import com.winsun.mapper.SysUserMapper;
import com.winsun.utils.MyBatisPlusUpdateUtils;
import com.winsun.utils.PicturesUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 二维码
 *
 * @Date: 2020/3/17 11:14
 */
@Slf4j
@RestController
@RequestMapping("QRCpde") //
public class QRCodeController extends BaseController {

    private static String PATHTYPE="poster";

    private static QRCodeMapper qrCodeMapper;

    private static SysUserMapper sysUserMapper;

    @Autowired
    public QRCodeController(QRCodeMapper qrCodeMapper, SysUserMapper sysUserMapper){
        QRCodeController.qrCodeMapper=qrCodeMapper;
        QRCodeController.sysUserMapper=sysUserMapper;
    }

    @Permission(menuname = "二维码上传图片",value = "picture", method = RequestMethod.POST)
    public ResponseData<String> uploadPicture(@RequestParam("picture") String picture) {
        ShiroUser user = getShiroUser();
        Integer userId=user.getId();
        String path = PicturesUtil.uploadPictures(picture, userId.toString(),PATHTYPE);
        QRCode qrCode = new QRCode();
        qrCode.setId(UUID.randomUUID().toString());
        qrCode.setCreateTime(new Date());
        qrCode.setPicture(path);
        qrCode.setUserId(userId);
        qrCode.setCreateName(user.getName());
        Integer insert = qrCodeMapper.insert(qrCode);
        if(insert == 1){
            return ResponseData.success("上传图片成功！");
        }
        return ResponseData.error("上传图片失败！");
    }

    /**
     *查询二维码图片列表
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Permission(menuname = "查询二维码图片列表", value = "list", method = RequestMethod.POST)
    public ResponseData<Page<QRCode>> listPicture(@RequestParam(name = "enable",required = false) String enable,@RequestParam(name = "pageNo") int pageIndex, @RequestParam(name = "pageSize") int pageSize) {
        ShiroUser user = getShiroUser();
        Wrapper<QRCode> wrapper = new EntityWrapper();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            wrapper.eq("user_id",user.getId());
        }
        wrapper.eq(StringUtils.isNotBlank(enable),"enable",enable);
        Page<QRCode> page = new Page<>(pageIndex, pageSize);
        List<QRCode> qrCodes = qrCodeMapper.selectPage(page, wrapper);
        if(qrCodes.size()==0){
            return ResponseData.error("无可查询数据或无数据权限！");
        }
        page.setRecords(qrCodes);
        return ResponseData.success(page, "查询成功！");
    }

    /**
     *移动端查询二维码图片列表
     * @param size 第几张图片
     * @return
     */
    @RequestMapping(value = "select", method = RequestMethod.POST)
    public ResponseData<List<QRCode>> selectPicture(String size) {
        Wrapper<QRCode> wrapper = new EntityWrapper();
        wrapper.eq("enable","1");
        List<String> objects = new ArrayList<>();
        objects.add("create_time");
        wrapper.orderDesc(objects);
        String str="limit "+size +","+(Integer.valueOf(size)+1);
        wrapper.last(str);
        List<QRCode> qrCodes = qrCodeMapper.selectList(wrapper);
        if(qrCodes.size()==0){
            return ResponseData.error("没有生成二维码的背景图！");
        }
        return ResponseData.success(qrCodes, "查询成功！");
    }
    /**
     *移动端查询海报图片数量
     * @return
     */
    @RequestMapping(value = "selectPictureCount", method = RequestMethod.POST)
    public ResponseData<Integer> selectPictureCount() {
        Wrapper<QRCode> wrapper = new EntityWrapper();
        wrapper.eq("enable","1");
        Integer integer = qrCodeMapper.selectCount(wrapper);
        if(integer==0){
            return ResponseData.error("没有生成二维码的背景图！");
        }
        return ResponseData.success(integer, "查询成功！");
    }


    /**
     * 删除二维码图片
     * @param id  需要删除的id
     * @return
     */
    @Permission(menuname = "删除二维码图片", value = "deletePicture/{id}", method = RequestMethod.POST)
    public ResponseData<String> deletePicture(@PathVariable("id") String id) {
    /*    ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }*/
        QRCode qrCode = qrCodeMapper.selectById(id);
        PicturesUtil.deletePictures(qrCode.getPicture());
        qrCodeMapper.deleteById(id);

        return ResponseData.success("删除成功！");
    }


    /**
     * 是否禁用二维码图片
     * @return
     */
    @Permission(menuname = "是否禁用二维码图片", value = "updateEnable/{id}", method = RequestMethod.POST)
    public ResponseData<String> updateEnable(@PathVariable("id") String id,@RequestParam(name = "enable") String enable) {
      /*  ShiroUser user = getShiroUser();
        if (!user.getRoleNames().stream().anyMatch(roleName -> StringUtils.equalsAny(roleName, "超级管理员"))) {
            return ResponseData.error("无数据权限");
        }*/
        Wrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        Map<String, Object> dataMapping = new HashMap<>();
        dataMapping.put("enable", enable);
        qrCodeMapper.updateForSet(MyBatisPlusUpdateUtils.toUpdateSet(dataMapping), wrapper);
        return ResponseData.success("修改成功！");
    }


}
