package com.jojubanking.boot.module.system.controller.admin.usecount;

import com.jojubanking.boot.module.system.dal.dataobject.usecount.UseCountToDO;
import com.jojubanking.boot.module.system.dal.dataobject.usecount.UseCountToDateDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.usecount.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.usecount.UseCountDO;
import com.jojubanking.boot.module.system.convert.usecount.UseCountConvert;
import com.jojubanking.boot.module.system.service.usecount.UseCountService;

@Api(tags = "管理后台 - 统计")
@RestController
@RequestMapping("/system/use-count")
@Validated
public class UseCountController {

    @Resource
    private UseCountService useCountService;

    @PostMapping("/create")
    @ApiOperation("创建统计")
    @PreAuthorize("@ss.hasPermission('system:use-count:create')")
    public CommonResult<Integer> createUseCount(@Valid @RequestBody UseCountCreateReqVO createReqVO) {
        return success(useCountService.createUseCount(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新统计")
    @PreAuthorize("@ss.hasPermission('system:use-count:update')")
    public CommonResult<Boolean> updateUseCount(@Valid @RequestBody UseCountUpdateReqVO updateReqVO) {
        useCountService.updateUseCount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除统计")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:use-count:delete')")
    public CommonResult<Boolean> deleteUseCount(@RequestParam("id") Integer id) {
        useCountService.deleteUseCount(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得统计")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('system:use-count:query')")
    public CommonResult<UseCountRespVO> getUseCount(@RequestParam("id") Integer id) {
        UseCountDO useCount = useCountService.getUseCount(id);
        return success(UseCountConvert.INSTANCE.convert(useCount));
    }

    @GetMapping("/list")
    @ApiOperation("获得统计列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:use-count:query')")
    public CommonResult<List<UseCountRespVO>> getUseCountList(@RequestParam("ids") Collection<Integer> ids) {
        List<UseCountDO> list = useCountService.getUseCountList(ids);
        return success(UseCountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得统计分页")
    @PreAuthorize("@ss.hasPermission('system:use-count:query')")
    public CommonResult<PageResult<UseCountRespVO>> getUseCountPage(@Valid UseCountPageReqVO pageVO) {
        PageResult<UseCountDO> pageResult = useCountService.getUseCountPage(pageVO);
        return success(UseCountConvert.INSTANCE.convertPage(pageResult));
    }
    @GetMapping("/counttosection")
    @ApiOperation("获得统计分页counttosection")
    @PreAuthorize("@ss.hasPermission('system:use-count:query')")
    public CommonResult<List<Long[]>> getUseCountToSection(@RequestParam String startDate1, @RequestParam String endDate1) throws ParseException, ParseException {
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        Date startDate = fmt.parse(startDate1.concat(" 00:00:00"));
        Date endDate = fmt.parse(endDate1.concat(" 23:59:59"));

        //功能标识：TL挂号、TH退号、YC住院、MZ门诊、WJ问卷、BG报告
        List<UseCountToDO> list = useCountService.getUseCountToSection(startDate, endDate);
        List<Long[]> resp = new ArrayList<>();
        //Long[] ans1 = new Long[6];
        Long[] ans1 = {0L, 0L, 0L, 0L, 0L, 0L};

        for(int i=0; i<list.size(); i++){
            if(list.get(i).getInterfaceIdentification().equals("TL")){
                ans1[0] = list.get(i).getTlsum();
            }
            else if(list.get(i).getInterfaceIdentification().equals("TH")){
                ans1[1] = list.get(i).getTlsum();
            }
            else if(list.get(i).getInterfaceIdentification().equals("MZ")){
                ans1[2] = list.get(i).getTlsum();
            }
            else if(list.get(i).getInterfaceIdentification().equals("BG")){
                ans1[3] = list.get(i).getTlsum();
            }
            else if(list.get(i).getInterfaceIdentification().equals("YC")){
                ans1[4] = list.get(i).getTlsum();
            }
            else if(list.get(i).getInterfaceIdentification().equals("WJ")){
                ans1[5] = list.get(i).getTlsum();
            }
        }
        resp.add(ans1);

        return success(resp);
    }

    @GetMapping("/countto")
    @ApiOperation("获得统计分页countto")
    @PreAuthorize("@ss.hasPermission('system:use-count:query')")
    public CommonResult<List<Long[]>> getUseCountTo(@Valid UseCountPageReqVO pageVO) {

        //7 days
        List<UseCountToDO> list = useCountService.getUseCountTo();
        List<UseCountToDO> list1 = useCountService.getUseCountTo1();
        List<UseCountToDO> list2 = useCountService.getUseCountTo2();
        List<UseCountToDO> list3 = useCountService.getUseCountTo3();
        List<UseCountToDO> list4 = useCountService.getUseCountTo4();
        List<UseCountToDO> list5 = useCountService.getUseCountTo5();
        List<UseCountToDO> list6 = useCountService.getUseCountTo6();

        List<Long[]> resp = new ArrayList<>();
        Long[] ans1 = {0L, 0L, 0L, 0L, 0L, 0L, 0L};
        for(int i=0; i<list.size(); i++){
            if(list.get(i).getInterfaceIdentification().equals("TL")){
                ans1[6] = list.get(i).getTlsum();
            }
        }
        for(int i=0; i<list1.size(); i++){
            if(list1.get(i).getInterfaceIdentification().equals("TL")){
                ans1[5] = list1.get(i).getTlsum();
            }
        }
        for(int i=0; i<list2.size(); i++){
            if(list2.get(i).getInterfaceIdentification().equals("TL")){
                ans1[4] = list2.get(i).getTlsum();
            }
        }
        for(int i=0; i<list3.size(); i++){
            if(list3.get(i).getInterfaceIdentification().equals("TL")){
                ans1[3] = list3.get(i).getTlsum();
            }
        }
        for(int i=0; i<list4.size(); i++){
            if(list4.get(i).getInterfaceIdentification().equals("TL")){
                ans1[2] = list4.get(i).getTlsum();
            }
        }
        for(int i=0; i<list5.size(); i++){
            if(list5.get(i).getInterfaceIdentification().equals("TL")){
                ans1[1] = list5.get(i).getTlsum();
            }
        }
        for(int i=0; i<list6.size(); i++){
            if(list6.get(i).getInterfaceIdentification().equals("TL")){
                ans1[0] = list6.get(i).getTlsum();
            }
        }
        resp.add(ans1);

        Long[] ans2 = {0L, 0L, 0L, 0L, 0L,0L, 0L};

        for(int i=0; i<list.size(); i++){
            if(list.get(i).getInterfaceIdentification().equals("TH")){
                ans2[6] = list.get(i).getTlsum();
            }
        }
        for(int i=0; i<list1.size(); i++){
            if(list1.get(i).getInterfaceIdentification().equals("TH")){
                ans2[5] = list1.get(i).getTlsum();
            }
        }
        for(int i=0; i<list2.size(); i++){
            if(list2.get(i).getInterfaceIdentification().equals("TH")){
                ans2[4] = list2.get(i).getTlsum();
            }
        }
        for(int i=0; i<list3.size(); i++){
            if(list3.get(i).getInterfaceIdentification().equals("TH")){
                ans2[3] = list3.get(i).getTlsum();
            }
        }
        for(int i=0; i<list4.size(); i++){
            if(list4.get(i).getInterfaceIdentification().equals("TH")){
                ans2[2] = list4.get(i).getTlsum();
            }
        }
        for(int i=0; i<list5.size(); i++){
            if(list5.get(i).getInterfaceIdentification().equals("TH")){
                ans2[1] = list5.get(i).getTlsum();
            }
        }
        for(int i=0; i<list6.size(); i++){
            if(list6.get(i).getInterfaceIdentification().equals("TH")){
                ans2[0] = list6.get(i).getTlsum();
            }
        }
        resp.add(ans2);


        Long[] ans3 = {0L, 0L, 0L, 0L, 0L,0L, 0L};

        for(int i=0; i<list.size(); i++){
            if(list.get(i).getInterfaceIdentification().equals("MZ")){
                ans3[6] = list.get(i).getTlsum();
            }
        }
        for(int i=0; i<list1.size(); i++){
            if(list1.get(i).getInterfaceIdentification().equals("MZ")){
                ans3[5] = list1.get(i).getTlsum();
            }
        }
        for(int i=0; i<list2.size(); i++){
            if(list2.get(i).getInterfaceIdentification().equals("MZ")){
                ans3[4] = list2.get(i).getTlsum();
            }
        }
        for(int i=0; i<list3.size(); i++){
            if(list3.get(i).getInterfaceIdentification().equals("MZ")){
                ans3[3] = list3.get(i).getTlsum();
            }
        }
        for(int i=0; i<list4.size(); i++){
            if(list4.get(i).getInterfaceIdentification().equals("MZ")){
                ans3[2] = list4.get(i).getTlsum();
            }
        }
        for(int i=0; i<list5.size(); i++){
            if(list5.get(i).getInterfaceIdentification().equals("MZ")){
                ans3[1] = list5.get(i).getTlsum();
            }
        }
        for(int i=0; i<list6.size(); i++){
            if(list6.get(i).getInterfaceIdentification().equals("MZ")){
                ans3[0] = list6.get(i).getTlsum();
            }
        }
        resp.add(ans3);


        Long[] ans4 = {0L, 0L, 0L, 0L, 0L,0L, 0L};

        for(int i=0; i<list.size(); i++){
            if(list.get(i).getInterfaceIdentification().equals("BG")){
                ans4[6] = list.get(i).getTlsum();
            }
        }
        for(int i=0; i<list1.size(); i++){
            if(list1.get(i).getInterfaceIdentification().equals("BG")){
                ans4[5] = list1.get(i).getTlsum();
            }
        }
        for(int i=0; i<list2.size(); i++){
            if(list2.get(i).getInterfaceIdentification().equals("BG")){
                ans4[4] = list2.get(i).getTlsum();
            }
        }
        for(int i=0; i<list3.size(); i++){
            if(list3.get(i).getInterfaceIdentification().equals("BG")){
                ans4[3] = list3.get(i).getTlsum();
            }
        }
        for(int i=0; i<list4.size(); i++){
            if(list4.get(i).getInterfaceIdentification().equals("BG")){
                ans4[2] = list4.get(i).getTlsum();
            }
        }
        for(int i=0; i<list5.size(); i++){
            if(list5.get(i).getInterfaceIdentification().equals("BG")){
                ans4[1] = list5.get(i).getTlsum();
            }
        }
        for(int i=0; i<list6.size(); i++){
            if(list6.get(i).getInterfaceIdentification().equals("BG")){
                ans4[0] = list6.get(i).getTlsum();
            }
        }
        resp.add(ans4);


        Long[] ans5 = {0L, 0L, 0L, 0L, 0L,0L, 0L};

        for(int i=0; i<list.size(); i++){
            if(list.get(i).getInterfaceIdentification().equals("YC")){
                ans5[6] = list.get(i).getTlsum();
            }
        }
        for(int i=0; i<list1.size(); i++){
            if(list1.get(i).getInterfaceIdentification().equals("YC")){
                ans5[5] = list1.get(i).getTlsum();
            }
        }
        for(int i=0; i<list2.size(); i++){
            if(list2.get(i).getInterfaceIdentification().equals("YC")){
                ans5[4] = list2.get(i).getTlsum();
            }
        }
        for(int i=0; i<list3.size(); i++){
            if(list3.get(i).getInterfaceIdentification().equals("YC")){
                ans5[3] = list3.get(i).getTlsum();
            }
        }
        for(int i=0; i<list4.size(); i++){
            if(list4.get(i).getInterfaceIdentification().equals("YC")){
                ans5[2] = list4.get(i).getTlsum();
            }
        }
        for(int i=0; i<list5.size(); i++){
            if(list5.get(i).getInterfaceIdentification().equals("YC")){
                ans5[1] = list5.get(i).getTlsum();
            }
        }
        for(int i=0; i<list6.size(); i++){
            if(list6.get(i).getInterfaceIdentification().equals("YC")){
                ans5[0] = list6.get(i).getTlsum();
            }
        }
        resp.add(ans5);


        Long[] ans6 = {0L, 0L, 0L, 0L, 0L,0L, 0L};

        for(int i=0; i<list.size(); i++){
            if(list.get(i).getInterfaceIdentification().equals("WJ")){
                ans6[6] = list.get(i).getTlsum();
            }
        }
        for(int i=0; i<list1.size(); i++){
            if(list1.get(i).getInterfaceIdentification().equals("WJ")){
                ans6[5] = list1.get(i).getTlsum();
            }
        }
        for(int i=0; i<list2.size(); i++){
            if(list2.get(i).getInterfaceIdentification().equals("WJ")){
                ans6[4] = list2.get(i).getTlsum();
            }
        }
        for(int i=0; i<list3.size(); i++){
            if(list3.get(i).getInterfaceIdentification().equals("WJ")){
                ans6[3] = list3.get(i).getTlsum();
            }
        }
        for(int i=0; i<list4.size(); i++){
            if(list4.get(i).getInterfaceIdentification().equals("WJ")){
                ans6[2] = list4.get(i).getTlsum();
            }
        }
        for(int i=0; i<list5.size(); i++){
            if(list5.get(i).getInterfaceIdentification().equals("WJ")){
                ans6[1] = list5.get(i).getTlsum();
            }
        }
        for(int i=0; i<list6.size(); i++){
            if(list6.get(i).getInterfaceIdentification().equals("WJ")){
                ans6[0] = list6.get(i).getTlsum();
            }
        }
        resp.add(ans6);


        return success(resp);
    }

    @GetMapping("/counttodate")
    @ApiOperation("获得统计分页counttodate")
    @PreAuthorize("@ss.hasPermission('system:use-count:query')")
    public CommonResult<String[]> getUseCountToDate() {
        List<UseCountToDateDO> list = useCountService.getUseCountToDate();
        String[] ans1 = new String[7];
        ans1[0] = list.get(0).getDate6();
        ans1[1] = list.get(0).getDate5();
        ans1[2] = list.get(0).getDate4();
        ans1[3] = list.get(0).getDate3();
        ans1[4] = list.get(0).getDate2();
        ans1[5] = list.get(0).getDate1();
        ans1[6] = list.get(0).getDate();

        return success(ans1);
        //return success(UseCountConvert.INSTANCE.convertCountToDate(list));
    }
    @GetMapping("/export-excel")
    @ApiOperation("导出统计 Excel")
    @PreAuthorize("@ss.hasPermission('system:use-count:export')")
    @OperateLog(type = EXPORT)
    public void exportUseCountExcel(@Valid UseCountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UseCountDO> list = useCountService.getUseCountList(exportReqVO);
        // 导出 Excel
        List<UseCountExcelVO> datas = UseCountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "统计.xls", "数据", UseCountExcelVO.class, datas);
    }

}
