package com.controller.intelligentdevice;


import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.service.intelligentdevice.EquipmentService;
import com.service.intelligentdevice.ReadingWorksService;
import com.tools.JwtUtils;

import java.util.List;
import java.util.Map;

@ControllerInterface(path="/readingWorks")
public class ReadingWorksController extends BaseController {
    ReadingWorksService service = ReadingWorksService.me;
    public void index() {
        render("/view/intelligentdevice/readingWorksList.html");
    }

    public void findReadingWorksList(){
        String token = this.getPara("token");
        int operatorid = JwtUtils.validateJWTForUserId(token);
        if(operatorid == 0){
            fail(3,"token错误");
            return;
        }
        int orgid = this.getParaToInt("orgid",0);
        if(orgid == 0) {
            fail(1,"请求参数不完整!");
            return;
        }
        Integer equipmentid = getParaToInt("equipmentid");
        String readerName = getPara("readerName");
        String title = getPara("title");
        String starttime = getPara("starttime");
        String endtime = getPara("endtime");
        Page page = service.findReadingWorksList(this.getParaToInt("pageNumber"),this.getParaToInt("pageSize"),equipmentid,orgid,readerName,title,starttime,endtime);
        List<Record> list = page.getList();
        for(Record record : list) {
            record.set("serverFileName",PropKit.get("readignroombaseurl")+record.get("serverFileName"));
        }
        success(page);
    }


    public void report(){
        render("/view/intelligentdevice/readingReport.html");
    }

    public void biokey(){
        render("/view/intelligentdevice/biokey.html");
    }

    public void readingbookTop20 (){
        Map info = JwtUtils.validateJWTForMap(this.getPara("token"));
        if(info == null){
            fail_token();
            return;
        }
        int orgid = (int)info.get("orgid");
        String begindate =getPara("begindate");
        String enddate =getPara("enddate")+" 23:59:59";

        success(service.readingbookTop20(begindate,enddate,orgid));
    }

    public void readingreaderTop20 (){
        Map info = JwtUtils.validateJWTForMap(this.getPara("token"));
        if(info == null){
            fail_token();
            return;
        }
        int orgid = (int)info.get("orgid");
        String begindate =getPara("begindate");
        String enddate =getPara("enddate")+" 23:59:59";
        success(service.readingreaderTop20(begindate,enddate,orgid));
    }

    public void readingresourceTop20 (){
        Map info = JwtUtils.validateJWTForMap(this.getPara("token"));
        if(info == null){
            fail_token();
            return;
        }
        int orgid = (int)info.get("orgid");
        String begindate =getPara("begindate");
        String enddate =getPara("enddate") +" 23:59:59";

        success(service.readingresourceTop20(begindate,enddate,orgid));
    }

    public void readingReprotData (){
        Map info = JwtUtils.validateJWTForMap(this.getPara("token"));
        if(info == null){
            fail_token();
            return;
        }
        int orgid = (int)info.get("orgid");
        String begindate =getPara("begindate");
        String enddate =getPara("enddate")+" 23:59:59";

        success(service.readingReprotData(begindate,enddate,orgid));
    }

}
