package com.ruoyi.web.controller.search;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.web.controller.week.SysWeekController;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Field;
import java.text.ParseException;
import java.util.*;

/**
 * @author Shawn
 */

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private SearchMapper searchMapper;

    @Autowired
    private SysWeekMapper weekMapper;

    @Autowired
    private IncomeMapper incomeMapper;

    @Autowired
    private SysBalanceMapper balanceMapper;

    @Autowired
    private B1Mapper b1Mapper;

    @Autowired
    private B2Mapper b2Mapper;

    @Autowired
    private B3Mapper b3Mapper;

    @Autowired
    private B4Mapper b4Mapper;

    @Autowired
    private B6Mapper b6Mapper;

    @Autowired
    private B7Mapper b7Mapper;

    @Autowired
    private B8Mapper b8Mapper;

    @Autowired
    private B9Mapper b9Mapper;

    @Autowired
    private B10Mapper b10Mapper;

    @Autowired
    private B11Mapper b11Mapper;

    @Autowired
    private B12Mapper b12Mapper;

    @Autowired
    private B13Mapper b13Mapper;

    @Autowired
    private B14Mapper b14Mapper;

    @Autowired
    private B15Mapper b15Mapper;

    @GetMapping("/selectDaily/{projectId}/{from}/{to}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<DailyReportStatus> weekSelect(@PathVariable String projectId, @PathVariable String from, @PathVariable String to) throws ParseException {
        List<String> monthList = new ArrayList<>();
        List<DailyReportStatus> Dailys = new ArrayList<>();
        for (String s : SysWeekController.getMonthBetween(from, to)) {
            Dailys.addAll(weekMapper.selectWeekNumByFromTo(projectId, from, to,s.replace("-","")));
        }
        return Dailys;
    }

    @GetMapping("/getB1/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B1> getB1(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B1 b1 = new B1();
        b1.setSurveyPerson(userId);
        b1.setProjectOwnerUnit(projectId);
        b1.setCurrentTime(dateAt);
        List<B1> b1s = new ArrayList<>();
        b1s = b1Mapper.selectB1ListByInfo(b1);
        return b1s;
    }

    @GetMapping("/getB2/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B2> getB2(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B2 b2 = new B2();
        b2.setXinTuPerson(userId);
        b2.setProjectOwnerUnit(projectId);
        b2.setCurrentTime(dateAt);
        List<B2> b2s;
        b2s = b2Mapper.selectListByInfo(b2);
        return b2s;
    }


    @GetMapping("/getB3/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B3> getB3(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B3 b3 = new B3();
        b3.setSurveyPerson(userId);
        b3.setProjectOwnerUnit(projectId);
        b3.setCurrentTime(dateAt);
        List<B3> b3s;
        b3s = b3Mapper.selectListByInfo(b3);
        return b3s;
    }


    @GetMapping("/getB4/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B4> getB4(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B4 b4 = new B4();
        b4.setNewPerson(userId);
        b4.setProjectOwnerUnit(projectId);
        b4.setCurrentTime(dateAt);
        List<B4> b4s;
        b4s = b4Mapper.selectListByInfo(b4);
        return b4s;
    }

    @GetMapping("/getB6/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B6> getB6(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B6 b6 = new B6();
        b6.setName(userId);
        b6.setProjectOwnerUnit(projectId);
        b6.setCurrentTime(dateAt);
        List<B6> b6s;
        b6s = b6Mapper.selectListByInfo(b6);
        return b6s;
    }


    @GetMapping("/getB7/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B7> getB7(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B7 b7 = new B7();
        b7.setSurveyPerson(userId);
        b7.setProjectOwnerUnit(projectId);
        b7.setCurrentTime(dateAt);
        List<B7> b7s;
        b7s = b7Mapper.selectListByInfo(b7);
        return b7s;
    }


    @GetMapping("/getB8/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B8> getB8(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B8 b8 = new B8();
        b8.setNewPerson(userId);
        b8.setProjectOwnerUnit(projectId);
        b8.setCurrentTime(dateAt);
        List<B8> b8s;
        b8s = b8Mapper.selectListByInfo(b8);
        return b8s;
    }


    @GetMapping("/getB9/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B9> getB9(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B9 b9 = new B9();
        b9.setNewPerson(userId);
        b9.setProjectOwnerUnit(projectId);
        b9.setCurrentTime(dateAt);
        List<B9> b9s;
        b9s = b9Mapper.selectListByInfo(b9);
        return b9s;
    }


    @GetMapping("/getB10/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B10> getB10(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B10 b10 = new B10();
        b10.setSurveyPerson(userId);
        b10.setProjectOwnerUnit(projectId);
        b10.setCurrentTime(dateAt);
        List<B10> b10s;
        b10s = b10Mapper.selectListByInfo(b10);
        return b10s;
    }


    @GetMapping("/getB11/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B11> getB11(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B11 b11 = new B11();
        b11.setNewPerson(userId);
        b11.setProjectOwnerUnit(projectId);
        b11.setCurrentTime(dateAt);
        List<B11> b11s;
        b11s = b11Mapper.selectListByInfo(b11);
        return b11s;
    }


    @GetMapping("/getB12/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B12> getB12(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B12 b12 = new B12();
        b12.setSurveyPerson(userId);
        b12.setProjectOwnerUnit(projectId);
        b12.setCurrentTime(dateAt);
        List<B12> b12s;
        b12s = b12Mapper.selectListByInfo(b12);
        return b12s;
    }


    @GetMapping("/getB13/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B13> getB13(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B13 b13 = new B13();
        b13.setNewPerson(userId);
        b13.setProjectOwnerUnit(projectId);
        b13.setCurrentTime(dateAt);
        List<B13> b13s;
        b13s = b13Mapper.selectListByInfo(b13);
        return b13s;
    }

    @GetMapping("/getB14/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B14> getB14(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B14 b14 = new B14();
        b14.setSurveyPerson(userId);
        b14.setProjectOwnerUnit(projectId);
        b14.setSurveyPlotDay(dateAt);
        List<B14> b14s;
        b14s = b14Mapper.selectListByInfo(b14);
        return b14s;
    }

    @GetMapping("/getB15/{userId}/{projectId}/{dateAt}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public List<B15> getB15(@PathVariable String userId, @PathVariable String projectId, @PathVariable String dateAt){
        B15 b15 = new B15();
        b15.setClaimPaymentPerson(userId);
        b15.setProjectOwnerUnit(projectId);
        b15.setExpenseDay(dateAt);
        List<B15> b15s;
        b15s = b15Mapper.selectListByInfo(b15);
        return b15s;
    }

    @PostMapping("/export")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public AjaxResult getExport(@RequestBody String weekInfo){
        List<DailyReportStatus> list = JSONArray.parseArray(weekInfo, DailyReportStatus.class);
        weekSum sum = new weekSum();
        List<B1> b1sum = new ArrayList<>();
        List<B2> b2sum = new ArrayList<>();
        List<B3> b3sum = new ArrayList<>();
        List<B4> b4sum = new ArrayList<>();
        List<B6> b6sum = new ArrayList<>();
        List<B7> b7sum = new ArrayList<>();
        List<B8> b8sum = new ArrayList<>();
        List<B9> b9sum = new ArrayList<>();
        List<B10> b10sum = new ArrayList<>();
        List<B11> b11sum = new ArrayList<>();
        List<B12> b12sum = new ArrayList<>();
        List<B13> b13sum = new ArrayList<>();
        List<B14> b14sum = new ArrayList<>();
        List<B15> b15sum = new ArrayList<>();
        for (DailyReportStatus dailyReportStatus : list) {
            B1 b1 = new B1(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B1> b1s = b1Mapper.selectB1ListByInfo(b1);
            if (b1s.size() != 0){
                b1sum.addAll(b1s);
            }

            B2 b2 = new B2(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B2> b2s = b2Mapper.selectListByInfo(b2);
            if (b2s.size() != 0){
                b2sum.addAll(b2s);
            }

            B3 b3 = new B3(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B3> b3s = b3Mapper.selectListByInfo(b3);
            if (b3s.size() != 0){
                b3sum.addAll(b3s);
            }

            B4 b4 = new B4(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B4> b4s = b4Mapper.selectListByInfo(b4);
            if (b4s.size() != 0){
                b4sum.addAll(b4s);
            }

            B6 b6 = new B6(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B6> b6s = b6Mapper.selectListByInfo(b6);
            if (b6s.size() != 0){
                b6sum.addAll(b6s);
            }

            B7 b7 = new B7(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B7> b7s = b7Mapper.selectListByInfo(b7);
            if (b7s.size() != 0){
                b7sum.addAll(b7s);
            }

            B8 b8 = new B8(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B8> b8s = b8Mapper.selectListByInfo(b8);
            if (b8s.size() != 0){
                b8sum.addAll(b8s);
            }

            B9 b9 = new B9(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B9> b9s = b9Mapper.selectListByInfo(b9);
            if (b9s.size() != 0){
                b9sum.addAll(b9s);
            }

            B10 b10 = new B10(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B10> b10s = b10Mapper.selectListByInfo(b10);
            if (b10s.size() != 0){
                b10sum.addAll(b10s);
            }

            B11 b11 = new B11(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B11> b11s = b11Mapper.selectListByInfo(b11);
            if (b11s.size() != 0){
                b11sum.addAll(b11s);
            }

            B12 b12 = new B12(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B12> b12s = b12Mapper.selectListByInfo(b12);
            if (b12s.size() != 0){
                b12sum.addAll(b12s);
            }

            B13 b13 = new B13(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B13> b13s = b13Mapper.selectListByInfo(b13);
            if (b13s.size() != 0){
                b13sum.addAll(b13s);
            }

            B14 b14 = new B14(dailyReportStatus.getDateAt(),dailyReportStatus.getUserId(),dailyReportStatus.getProjectId());
            List<B14> b14s = b14Mapper.selectListByInfo(b14);
            if (b14s.size() != 0){
                b14sum.addAll(b14s);
            }


            B15 b15 = new B15(dailyReportStatus.getDateAt(),dailyReportStatus.getProjectId(),dailyReportStatus.getUserId());
            List<B15> b15s = b15Mapper.selectListByInfo(b15);
            if (b15s.size() != 0){
                b15sum.addAll(b15s);
            }

        }
        sum.setB1s(b1sum);
        sum.setB2s(b2sum);
        sum.setB3s(b3sum);
        sum.setB4s(b4sum);
        sum.setB6s(b6sum);
        sum.setB7s(b7sum);
        sum.setB8s(b8sum);
        sum.setB9s(b9sum);
        sum.setB10s(b10sum);
        sum.setB11s(b11sum);
        sum.setB12s(b12sum);
        sum.setB13s(b13sum);
        sum.setB14s(b14sum);
        sum.setB15s(b15sum);
        return AjaxResult.success(sum);
    }


    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    @GetMapping("/getSalary/{unitNum}/{date}")
    public List<Income> getListByTime(@PathVariable String unitNum,@PathVariable String date) {
        return incomeMapper.selectByTime(unitNum,date.split(",")[0],date.split(",")[1]);
    }

    @GetMapping("/getbalance/{unittype}/{unitNum}/{date}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public AjaxResult getBalance(@PathVariable String unittype,@PathVariable String unitNum,@PathVariable String date){

//        System.out.println(unittype);
//        System.out.println(unitNum);
//        System.out.println(date);
        List<BalanceDay> balanceDay = balanceMapper.getBalanceDay(unittype, unitNum, date.split(",")[0], date.split(",")[1]);
        List<BalancePiece> balancePiece = balanceMapper.getBalancePiece(unittype, unitNum, date.split(",")[0], date.split(",")[1]);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("balancePiece",balancePiece);
        ajaxResult.put("balanceDay",balanceDay);
        return ajaxResult;
    }

    @GetMapping("/getColumns/{tableName}")
//    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public AjaxResult getColumns(@PathVariable String tableName) throws ClassNotFoundException {
        List<String> columns = searchMapper.getColumns(tableName);
        Class<?> calss;
        if(tableName.contains("_")){
            calss = Class.forName("com.ruoyi.system.domain."+tableName.split("_")[0].substring(0,1).toUpperCase()+tableName.split("_")[0].substring(1)+tableName.split("_")[1].substring(0,1).toUpperCase()+tableName.split("_")[1].substring(1));
        }else{
            calss = Class.forName("com.ruoyi.system.domain."+tableName.substring(0,1).toUpperCase()+tableName.substring(1));
        }
        Field[] fields = calss.getDeclaredFields();
        List<String> props = new ArrayList<>();
        for (Field field : fields) {
            props.add(field.getName().split("\\.")[field.getName().split("\\.").length-1]);
        }
//        System.out.println(columns);
//        System.out.println(columns.size());
//        System.out.println(props);
//        System.out.println(props.size());
        LinkedHashMap<String,String> finalFileds = new LinkedHashMap<>();
        for (int i = 0; i < columns.size(); i++) {
            finalFileds.put(columns.get(i),props.get(i));
        }
        AjaxResult ajaxResult = AjaxResult.success(finalFileds);
        ajaxResult.put("columns",columns);
        ajaxResult.put("props",props);
        return ajaxResult;
    }


    @GetMapping("/gettableData/{tableName}/{field}/{logic}/{fieldInfo}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public AjaxResult gettableData(@PathVariable String tableName,@PathVariable String field,@PathVariable String logic,@PathVariable String fieldInfo) throws ClassNotFoundException {
        if("小于".equals(logic)){
            logic = "<";
        }
        if("大于".equals(logic)){
            logic = ">";
        }
        if("小于等于".equals(logic)){
            logic = "<=";
        }
        if("大于等于".equals(logic)){
            logic = ">=";
        }
        Class<?> calss;
        if(tableName.contains("_")){
            calss = Class.forName("com.ruoyi.system.domain."+tableName.split("_")[0].substring(0,1).toUpperCase()+tableName.split("_")[0].substring(1)+tableName.split("_")[1].substring(0,1).toUpperCase()+tableName.split("_")[1].substring(1));
        }else{
            calss = Class.forName("com.ruoyi.system.domain."+tableName.substring(0,1).toUpperCase()+tableName.substring(1));
        }
        List<JSONObject> tableData = searchMapper.getTableData(tableName, field, logic, fieldInfo);
//        System.out.println(tableData.toString());
        List<Object> tableDataList = new ArrayList<>();
        for (JSONObject tableDatum : tableData) {
//            System.out.println(JSONObject.parseObject(JSON.toJSONStringWithDateFormat(tableDatum,"yyyy-MM-dd HH:mm:ss"), calss).toString());
            tableDataList.add(JSONObject.parseObject(JSON.toJSONStringWithDateFormat(tableDatum,"yyyy-MM-dd HH:mm:ss"), calss));
        }
        return AjaxResult.success(tableDataList);
    }

    @GetMapping("/gettableData2/{tableName}/{field}/{logic}/{fieldInfo}/{field2}/{fieldInfo2}/{fieldInfo3}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public AjaxResult gettableData2(@PathVariable String tableName,@PathVariable String field,@PathVariable String logic,@PathVariable String fieldInfo,@PathVariable String field2,@PathVariable String fieldInfo2,@PathVariable String fieldInfo3) throws ClassNotFoundException {
        if("小于".equals(logic)){
            logic = "<";
        }
        if("大于".equals(logic)){
            logic = ">";
        }
        if("小于等于".equals(logic)){
            logic = "<=";
        }
        if("大于等于".equals(logic)){
            logic = ">=";
        }
        Class<?> calss;
        if(tableName.contains("_")){
            calss = Class.forName("com.ruoyi.system.domain."+tableName.split("_")[0].substring(0,1).toUpperCase()+tableName.split("_")[0].substring(1)+tableName.split("_")[1].substring(0,1).toUpperCase()+tableName.split("_")[1].substring(1));
        }else{
            calss = Class.forName("com.ruoyi.system.domain."+tableName.substring(0,1).toUpperCase()+tableName.substring(1));
        }
        List<JSONObject> tableData = searchMapper.getTableData2(tableName, field, logic, fieldInfo,field2,fieldInfo2,fieldInfo3);
//        System.out.println(tableData.toString());
        List<Object> tableDataList = new ArrayList<>();
        for (JSONObject tableDatum : tableData) {
//            System.out.println(JSONObject.parseObject(JSON.toJSONStringWithDateFormat(tableDatum,"yyyy-MM-dd HH:mm:ss"), calss).toString());
            tableDataList.add(JSONObject.parseObject(JSON.toJSONStringWithDateFormat(tableDatum,"yyyy-MM-dd HH:mm:ss"), calss));
        }
        return AjaxResult.success(tableDataList);
    }

    @GetMapping("/gettableData3/{tableName}/{field2}/{fieldInfo2}/{fieldInfo3}")
    @PreAuthorize("@ss.hasPermi('search:comprehensive:manage')")
    public AjaxResult gettableData3(@PathVariable String tableName,@PathVariable String field2,@PathVariable String fieldInfo2,@PathVariable String fieldInfo3) throws ClassNotFoundException {
        Class<?> calss;
        if(tableName.contains("_")){
            calss = Class.forName("com.ruoyi.system.domain."+tableName.split("_")[0].substring(0,1).toUpperCase()+tableName.split("_")[0].substring(1)+tableName.split("_")[1].substring(0,1).toUpperCase()+tableName.split("_")[1].substring(1));
        }else{
            calss = Class.forName("com.ruoyi.system.domain."+tableName.substring(0,1).toUpperCase()+tableName.substring(1));
        }
        List<JSONObject> tableData = searchMapper.getTableData3(tableName,field2,fieldInfo2,fieldInfo3);
//        System.out.println(tableData.toString());
        List<Object> tableDataList = new ArrayList<>();
        for (JSONObject tableDatum : tableData) {
//            System.out.println(JSONObject.parseObject(JSON.toJSONStringWithDateFormat(tableDatum,"yyyy-MM-dd HH:mm:ss"), calss).toString());
            tableDataList.add(JSONObject.parseObject(JSON.toJSONStringWithDateFormat(tableDatum,"yyyy-MM-dd HH:mm:ss"), calss));
        }
        return AjaxResult.success(tableDataList);
    }
}
