package com.iformall.controller.sys;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.WxBusiness;
import com.iformall.domain.po.WxSubBusiness;
import com.iformall.domain.vo.WxBusinessDataVo;
import com.iformall.service.ExcelService;
import com.iformall.service.WxBusinessService;
import com.iformall.service.WxSubBusinessService;
import com.iformall.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("wxBusiness")
@Api(description = "业态接口")
public class WxBusinessController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxBusinessService wxBusinessService;
    @Autowired
    private WxSubBusinessService wxSubBusinessService;
    @Autowired
    private ExcelService excelService;


    @ApiOperation("业态全列表接口")
    @GetMapping("listAll")
    @SystemControllerLog(description = "业态-全列表")
    public ResultData listAll() {
        logger.debug("[" + getIpAddr() + "] WxBusinessController::getList");
        final List<WxBusiness> busList = wxBusinessService.findListAll(getTenantId(),null);
        return new ResultData(busList);
    }

    @ApiOperation("子业态表接口")
    @GetMapping("listSub")
    @SystemControllerLog(description = "子业态")
    public ResultData listSub(@ModelAttribute WxSubBusiness wxSubBusiness) {
        logger.debug("[" + getIpAddr() + "] WxBusinessController::getList");
        final List<WxSubBusiness> busList = wxSubBusinessService.findList(wxSubBusiness);
        return new ResultData(busList);
    }

    @ApiOperation("业态分析接口")
    @GetMapping("businessDataList")
    @SystemControllerLog(description = "业态分析接口")
    public ResultData businessDataList(@ModelAttribute WxBusiness wxBusiness) throws Exception{
        logger.debug("[" + getIpAddr() + "] WxBusinessController::businessDataList");
        wxBusiness.setTenantId(getTenantId());
        if(wxBusiness.getStartdate() == null || wxBusiness.getEnddate() == null){
            Date sDate = DateUtils.getFirstDayForCurrMonth(DateUtils.getLastMonthToDay());
            Date eDate = DateUtils.getLastDayForMonth(DateUtils.getLastMonthToDay());
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            wxBusiness.setStartdate(sdT.parse(sd.format(sDate)+" 00:00:00"));
            wxBusiness.setEnddate(sdT.parse(sd.format(eDate)+" 23:59:59"));
        }

        List<Map<String,Object>> mapList = wxBusinessService.businessDataList(wxBusiness,getUserId());
        for (Map<String,Object> map:mapList) {
            if(map.get("rent") == null){
                map.put("rent",0);
            }
            if(map.get("sale") == null){
                map.put("sale",0);
                map.put("rentSaleRate",0);
                continue;
            }
            BigDecimal sale =  new BigDecimal(map.get("sale").toString());
            BigDecimal rent =  new BigDecimal(map.get("rent").toString());
            if(sale.doubleValue() <= 0){
                map.put("rentSaleRate",0);
                continue;
            }else{
                BigDecimal divide = rent.divide(sale, 2, BigDecimal.ROUND_HALF_UP);
                map.put("rentSaleRate", divide);
            }
        }
        return new ResultData(mapList);
    }

    @ApiOperation("导出业态数据列表")
    @GetMapping("exportBusinessDataList")
    @SystemControllerLog(description = "导出业态数据列表")
    public void exportBusinessDataList(@ModelAttribute WxBusiness wxBusiness, HttpServletResponse response) throws Exception{
        logger.debug("[" + getIpAddr() + "] WxBusinessController::exportBusinessDataList");
        wxBusiness.setTenantId(getTenantId());
        if(wxBusiness.getStartdate() == null || wxBusiness.getEnddate() == null){
            Date sDate = DateUtils.getFirstDayForCurrMonth(DateUtils.getLastMonthToDay());
            Date eDate = DateUtils.getLastDayForMonth(DateUtils.getLastMonthToDay());
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            wxBusiness.setStartdate(sdT.parse(sd.format(sDate)+" 00:00:00"));
            wxBusiness.setEnddate(sdT.parse(sd.format(eDate)+" 23:59:59"));
        }
        List<Map<String,Object>> dataList = wxBusinessService.businessDataList(wxBusiness,getUserId());
        List<WxBusinessDataVo> exeList = new ArrayList<>();
        for (Map<String,Object> m:dataList) {
            WxBusinessDataVo businessDataVo = new WxBusinessDataVo();
            businessDataVo.setName((String)m.get("title"));
            if(m.get("comPrice") != null)
                businessDataVo.setComPrice(m.get("comPrice").toString());
            if(m.get("rent") != null)
                businessDataVo.setRentRate(m.get("rent").toString());
            if(m.get("rentSaleRate") != null)
                businessDataVo.setRentSaleRate(m.get("rentSaleRate").toString());
            if(m.get("volume") != null) {
                //单位转万
                BigDecimal b = (BigDecimal)m.get("volume");
                String dstr = b.divide(new BigDecimal(10000)).setScale(4, BigDecimal.ROUND_HALF_UP).toString();
                businessDataVo.setSale(dstr);
            }
            if(m.get("sale") != null)
                businessDataVo.setSaleRate(m.get("sale").toString());

            if(m.get("rent") == null){
                m.put("rent",0);
            }
            if(m.get("sale") == null){
                m.put("sale",0);
                m.put("rentSaleRate",0);
                businessDataVo.setRentSaleRate("0");
                exeList.add(businessDataVo);
                continue;
            }
            BigDecimal sale =  new BigDecimal(m.get("sale").toString());
            BigDecimal rent =  new BigDecimal(m.get("rent").toString());
            if(sale.doubleValue() <= 0){
                businessDataVo.setRentSaleRate("0");
                exeList.add(businessDataVo);
                continue;
            }else {
                BigDecimal divide = rent.divide(sale, 2, BigDecimal.ROUND_HALF_UP);
                m.put("rentSaleRate", divide);
                businessDataVo.setRentSaleRate(divide.toString());
            }
            exeList.add(businessDataVo);
        }
        excelService.exportExcel(exeList, null, "业态统计", WxBusinessDataVo.class, "业态统计列表.xlsx", response, false);
    }

    @ApiOperation("贡献率排行")
    @GetMapping("devoteList")
    @SystemControllerLog(description = "贡献率排行")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData devoteList(@ModelAttribute WxBusiness wxBusiness,Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxBusinessController::devoteList");
        wxBusiness.setTenantId(getTenantId());
        return new ResultData(wxBusinessService.devoteList(wxBusiness,pageNum,pageSize));
    }


}
