package cn.pluss.platform.controller.qrCode;

import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entity.QrCode;
import cn.pluss.platform.qrCode.QrCodeService;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@Controller
@RequestMapping("qrCode")
public class QrCodeController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private QrCodeService qrCodeService;


    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "qrCode/qrCodeList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryQrCodeByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryQrCodeByPage(@RequestParam(defaultValue = "1") Integer currPage,
                                                           QrCode qrCode) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        Map<String, Object> result = new HashMap<String, Object>(16);
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        if (StringUtil.isNotEmpty(qrCode.getQrNo())) {
            map.put("qrNo", qrCode.getQrNo());
        }
        if (StringUtil.isNotEmpty(qrCode.getStatus())) {
            map.put("status", qrCode.getStatus());
        }
        Boolean bol = verifyUserRole();
        if (bol) {
            List<QrCode> qrCodeList = qrCodeService.queryQrCodePage(map);
            Integer count = qrCodeService.queryQrCodePageCount(map);
            result.put("qrCodeList", qrCodeList);
            result.put("count", count);
            result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
            result.put("currPage", currPage);
        }
        return result;
    }

    @RequestMapping(value = "/saveQrCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveQrCode(QrCode qrCode) {
        Map<String, Object> result = new HashMap<String, Object>();
        if (qrCode.getId() != null && qrCode.getId() != 0) {
            qrCodeService.updateQrCode(qrCode);
        } else {
            if(StringUtil.isEmpty(qrCode.getQrNo())){
                result.put("resultCode", "2");
                result.put("resultMessage", "二维码编号不能为空!");
                return result;
            }
            Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
            boolean qrNoCheck = pattern.matcher(qrCode.getQrNo()).matches();
            if(!qrNoCheck){
                result.put("resultCode", "4");
                result.put("resultMessage", "输入编号不合法！");
                return result;
            }
            QrCode checkQrCode = qrCodeService.getQrCodeByQrNo(qrCode.getQrNo());
            if(checkQrCode != null){
                result.put("resultCode", "3");
                result.put("resultMessage", "二维码编号已存在，请重新输入!");
                return result;
            }
            qrCode.setCreateBy(queryCurrentShiroUser().getName());
            qrCodeService.saveQrCode(qrCode);
        }
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 范围新增
     * @param qrNoStart
     * @param qrNoEnd
     * @return
     */
    @RequestMapping(value = "/saveBatchQrCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveBatchQrCode(String qrNoStart,String qrNoEnd) {
        Map<String, Object> result = new HashMap<String, Object>();
        if(StringUtil.isEmpty(qrNoStart) || StringUtil.isEmpty(qrNoEnd)){
            result.put("resultCode", "2");
            result.put("resultMessage", "范围值不能为空!");
        }
        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");
        boolean matchesStart = pattern.matcher(qrNoStart).matches();
        boolean matchesEnd = pattern.matcher(qrNoEnd).matches();
        if(matchesStart && matchesEnd){
            Integer startNum = Integer.valueOf(qrNoStart);
            Integer endNum = Integer.valueOf(qrNoEnd);
            if(startNum>endNum){
                result.put("resultCode", "3");
                result.put("resultMessage", "二维码编号范围输入有误!");
                return result;
            }
            List<QrCode> qrCodeList = new ArrayList<>();
            List<String> checkQrNo = new ArrayList<>();
            for (int i = startNum;i<=endNum;i++){
                QrCode qrCode = new QrCode();
                qrCode.setCreateBy(queryCurrentShiroUser().getName());
                qrCode.setQrNo(String.valueOf(i));
                qrCodeList.add(qrCode);
                checkQrNo.add(String.valueOf(i));
            }
            Integer checkCount = qrCodeService.checkQrNo(checkQrNo);
            if(checkCount > 0){
                result.put("resultCode", "4");
                result.put("resultMessage", "范围中包含已入库的编号，请重新输入!");
                return result;
            }
            qrCodeService.batchSaveQrCode(qrCodeList);
            result.put("resultCode", "1");
            result.put("resultMessage", "导入成功!");
            return result;
        }else{
            result.put("resultCode", "2");
            result.put("resultMessage", "范围值输入格式有误!");
            return result;
        }
    }

    @RequestMapping(value = "/deleteBatchQrCode", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteBatchQrCode(String[] ids) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        if(ids.length == 0){
            result.put("resultCode", "2");
            result.put("resultMessage", "请选中需要删除的数据!");
            return result;
        }
        qrCodeService.deleteQrCodeByIds(ids);
        result.put("resultCode", "1");
        result.put("resultMessage", "操作成功!");
        return result;
    }
}
