package com.yunzhi.marketing.xlk.controller;


import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.dto.StatisticsDTO;
import com.yunzhi.marketing.entity.TaPerson;
import com.yunzhi.marketing.service.ChannelCustomerStatisticsService;
import com.yunzhi.marketing.service.ITaPersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api")
@Api(value = "森哥看这里-合作渠道统计表", tags = "森哥看这里-xlk-合作渠道统计表")
public class ChannelCustomerStatisticsController extends BaseController {

    @Autowired
    public ITaPersonService taPersonService;

    @Autowired
    private ChannelCustomerStatisticsService channelCustomerStatisticsService;

    @ApiOperation(value = "wx-龙虎榜排名数据", notes = "wx-龙虎榜排名数据")
    @GetMapping(value="/wx/ranklist")
    public ResponseBean getRanklist(@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                    @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
                                    @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        String openid = getOpenId(request);
        Integer orgId = getOrgId(request);
        List<TaPerson> persons = taPersonService.getPersonsByOpenId(openid);
        if (null == persons || persons.size() == 0) {
            return ResponseBean.error("当前账户信息异常, 清除缓存重试", ResponseBean.ERROR_UNAVAILABLE);
        }
        TaPerson person = persons.get(0);

        // 构造参数
        StatisticsDTO statisticsDTo = new StatisticsDTO();
        statisticsDTo.setStartDate(startDate);
        statisticsDTo.setEndDate(endDate);
        statisticsDTo.setOrgId(orgId);
        statisticsDTo.setPersonId(person.getPersonId());
        return  channelCustomerStatisticsService.getRanklist(statisticsDTo);
    }

    @ApiOperation(value = "wx-销售简报", notes = "wx-销售简报")
    @GetMapping(value="/wx/briefing")
    public ResponseBean getBriefing(@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                    @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate,
                                    @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        String openid = getOpenId(request);
        Integer orgId = getOrgId(request);
        List<TaPerson> persons = taPersonService.getPersonsByOpenId(openid);
        if (null == persons || persons.size() == 0) {
            return ResponseBean.error("当前账户信息异常, 清除缓存重试", ResponseBean.ERROR_UNAVAILABLE);
        }
        TaPerson person = persons.get(0);

        // 构造参数
        StatisticsDTO statisticsDTo = new StatisticsDTO();
        statisticsDTo.setStartDate(startDate);
        statisticsDTo.setEndDate(endDate);
        statisticsDTo.setOrgId(orgId);
        statisticsDTo.setPersonId(person.getPersonId());
        return  channelCustomerStatisticsService.getBriefing(statisticsDTo);
    }

    @ApiOperation(value = "wx-销售简报折线图", notes = "wx-销售简报折线图")
    @GetMapping(value="/wx/briefing/detail")
    public ResponseBean getBriefingDetail(@RequestParam(value = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
                                    @RequestParam(value = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate, @RequestParam(value = "customerType") String customerType,
                                    @RequestParam(value = "type") String type,
                                    @RequestHeader("authorization") String token,HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        String openid = getOpenId(request);
        Integer orgId = getOrgId(request);
        List<TaPerson> persons = taPersonService.getPersonsByOpenId(openid);
        if (null == persons || persons.size() == 0) {
            return ResponseBean.error("当前账户信息异常, 清除缓存重试", ResponseBean.ERROR_UNAVAILABLE);
        }
        TaPerson person = persons.get(0);

        // 构造参数
        StatisticsDTO statisticsDTo = new StatisticsDTO();
        statisticsDTo.setStartDate(startDate);
        statisticsDTo.setEndDate(endDate);
        statisticsDTo.setOrgId(orgId);
        statisticsDTo.setPersonId(person.getPersonId());
        statisticsDTo.setType(type);
        statisticsDTo.setCustomerType(customerType);
        return  channelCustomerStatisticsService.getBriefingDetail(statisticsDTo);
    }
}
