package com.chinaweal.aiccs.crgs.entbase.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chinaweal.aiccs.aiccs.system.entity.TRBaseCode;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.crgs.entbase.service.EOtNcaseService;
import com.chinaweal.aiccs.crgs.transfor.entity.dto.StatisticLoadDto;
import com.chinaweal.aiccs.crgs.transfor.service.OtNcaseDownloadLogService;
import com.chinaweal.aiccs.crgs.transfor.service.OtNcaseUploadLogService;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 其他部门_行政处罚基本信息_2019 前端控制器
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-10-20
 */
@Api("行政处罚相关接口")
@RestController
@RequestMapping("/ncase")
public class EOtNcaseController extends BaseController {

    @Autowired
    private EOtNcaseService eOtNcaseService;
    @Autowired
    private OtNcaseDownloadLogService otNcaseDownloadLogService;
    @Autowired
    private OtNcaseUploadLogService otNcaseUploadLogService;
    @Autowired
    private TRBaseCodeService baseCodeService;

    /**
     * 分页
     * @param request   请求
     * @param pageRequestDto    分页参数
     * @return  分页结果
     * @author lroyia
     * @since  2021年10月20日 14:33:18
     */
    @PostMapping("page")
    public RestResult<?> getPage(HttpServletRequest request, @RequestBody PageRequestDto pageRequestDto){
        return RestResult.ok(eOtNcaseService.defaultPaging(pageRequestDto, null, null));
    }

    /**
     * 统计行政处罚信息上传和下载记录
     * @param startDate 统计开始时间
     * @param endDate   统计结束时间
     * @return  统计结果
     * @author lroyia
     * @since  2021年11月8日 16:13:00
     */
    @ApiOperation("统计行政处罚信息上传和下载记录")
    @PostMapping("statistic")
    private RestResult<?> statistic(@ApiParam("统计开始日期") String startDate, @ApiParam("统计结束日期") String endDate){
        List<TRBaseCode> codeList = baseCodeService.list(new LambdaQueryWrapper<TRBaseCode>()
                .eq(TRBaseCode::getCodeid, "CA01")
                .likeLeft(TRBaseCode::getCode, "0000").orderByAsc(TRBaseCode::getCode));

        List<StatisticLoadDto> uploadList = otNcaseUploadLogService.statisticUploadCount(startDate, endDate);
        List<StatisticLoadDto> downLoadList = otNcaseDownloadLogService.statisticDownloadCount(startDate, endDate);
        Map<String, Map<String, Object>> result = new LinkedHashMap<>();
        for (TRBaseCode each : codeList) {
            Map<String, Object> atom = new HashMap<>();
            atom.put("name", each.getName());
            atom.put("upload1", 0);
            atom.put("download0", 0);
            atom.put("download1", 0);
            result.put(each.getCode(), atom);
        }

        // 上传量
        for (StatisticLoadDto each : uploadList) {
            Map<String, Object> atom = result.get(each.getAreaCode());
            atom.put("upload1", each.getTotalCount());
        }

        // 下载量，和待下载量
        for (StatisticLoadDto each : downLoadList) {
            Map<String, Object> atom = result.get(each.getAreaCode());
            if(atom == null) continue;
            // 已下载
            if(Constant.TRUE_1_STR.equals(each.getFlag())){
                atom.put("download1", each.getTotalCount());
            }else if(Constant.FALSE_0_STR.equals(each.getFlag())){
                // 未下载
                atom.put("download0", each.getTotalCount());
            }
        }

        return RestResult.ok(result);
    }
}

