package com.nantian.cloud.ggjg.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.ggjg.entity.Mlk;
import com.nantian.cloud.ggjg.service.MlkService;
import com.nantian.sc.cloud.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/mlk")
@Api(value = "mlk", tags = "名录库")
public class MlkController {

    private MlkService mlkService;

    @GetMapping("/page")
    @ApiOperation(value = "名录库", notes = "分页获取名录库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "mlk", value = "名录库对象", required = false, dataType = "json", paramType = "path")
    })
    public R getMlkPage(Page page, Mlk mlk) {
        return R.ok(mlkService.page(page, Wrappers.query(mlk)));
    }

    @GetMapping("/tjb")
    @ApiOperation(value = "名录库", notes = "获取名录库统计表")
    @ApiImplicitParams({
               @ApiImplicitParam(name = "mlk", value = "名录库对象", required = false, dataType = "json", paramType = "path")
    })
    public R getMlkTjb(Mlk mlk) {
        HashMap<String,String> map =new HashMap<String,String>();
        if(mlk.getBusinessTime() ==null){
            mlk.setBusinessTime(new Long("2020"));
        }
        if(mlk.getBusinessTime().longValue()<new Long(100000).longValue()){
            System.out.println(mlk.getBusinessTime().toString()+"01");
            mlk.setBusinessTime(new Long(mlk.getBusinessTime().toString().concat("01")));
        }
        System.out.println(mlk.getBusinessTime());
        System.out.println(mlk.getBusinessTime().toString());
        map.put("businessTime",mlk.getBusinessTime().toString());

        Map<String, Object> result = new HashMap<String, Object>();
        List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
        if(mlk.getCode()!=null && !"".equals(mlk.getCode())){
            result.put("area",mlkService.getAreaByCode(mlk.getCode()));
            if(mlk.getCode().contains("0000")){//省
                map.put("code",StrUtil.subWithLength(mlk.getCode(), 0, 2));
                resultMap = mlkService.queryTjb(map);
            }else if(mlk.getCode().contains("00")){//州市
                map.put("code",StrUtil.subWithLength(mlk.getCode(), 0, 4));
                resultMap = mlkService.queryTjbCity(map);
            }else{//区县
                map.put("code",mlk.getCode());
                resultMap = mlkService.queryTjbCounty(map);
            }
        }else{
            result.put("area","云南省");
            resultMap = mlkService.queryTjb(map);
        }

        /*if(StringUtils.isEmpty(map.get("businessTime"))){
            map.put("businessTime","2020");
        }
        if(4==map.get("businessTime").length()){
            map.put("businessTime",map.get("businessTime")+"01");
        }*/

        result.put("data",resultMap);
        return R.ok(result);
        //return R.ok(mlkService.listObjs());
    }

}
