package com.slodon.b2b2c.controller.statistics.seller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.slodon.b2b2c.core.constant.EnquiryConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.enums.StatsTimeType;
import com.slodon.b2b2c.member.example.MemberEnquiryExample;
import com.slodon.b2b2c.member.pojo.MemberEnquiry;
import com.slodon.b2b2c.model.member.advich.MemberEnquiryModel;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.statistics.EnquiryOverviewVO;
import com.slodon.b2b2c.vo.statistics.EnquiryTrendVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Api(tags = "seller-交易分析")
@RestController
@RequestMapping("v3/statistics/seller/enquiry/analysis")
public class SellerEnquiryAnalysisController extends BaseController {

    @Resource
    private MemberEnquiryModel memberEnquiryModel;

    @ApiOperation("询盘总览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间", required = true),
            @ApiImplicitParam(name = "endTime", value = "结束时间", required = true),
            @ApiImplicitParam(name = "webSite", value = "站点")
    })
    @GetMapping("enquiryOverview")
    public JsonResult<EnquiryOverviewVO> enquiryOverview(HttpServletRequest request, @RequestParam(value = "webSite", required = false,defaultValue = "1") String webSite) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        EnquiryOverviewVO enquiryOverviewVO = new EnquiryOverviewVO();
        //询盘数据
        List<String> itemTypes = new ArrayList<>();
        itemTypes.add(EnquiryConst.ENQUIRY_ITEM_TYPE_GOODS);
        itemTypes.add(EnquiryConst.ENQUIRY_ITEM_TYPE_SHOP);
        String itemTypeStr = StringUtil.listToString(itemTypes);

        //询盘总数
        MemberEnquiryExample memberEnquiryTotalExample = new MemberEnquiryExample();
        memberEnquiryTotalExample.setStoreId(vendor.getStoreId());
        memberEnquiryTotalExample.setStatusNotEqual(EnquiryConst.ENQUIRY_DELETE);
        memberEnquiryTotalExample.setItemTypeIn(itemTypeStr);
        memberEnquiryTotalExample.setWebSite(webSite);
        enquiryOverviewVO.setEnquiryTotalNum(memberEnquiryModel.getMemberEnquiryCount(memberEnquiryTotalExample));

        //新增询盘数
        Date enquiryEndTime = new Date();
        DateTime enquiryStartTime = DateUtil.beginOfDay(enquiryEndTime);
        MemberEnquiryExample memberEnquiryNewExample = new MemberEnquiryExample();
        memberEnquiryNewExample.setStoreId(vendor.getStoreId());
        memberEnquiryNewExample.setCreateTimeGte(com.slodon.b2b2c.core.util.DateUtil.dateToString(enquiryStartTime, com.slodon.b2b2c.core.util.DateUtil.DATE_FORMAT_FIVE));
        memberEnquiryNewExample.setCreateTimeLte(com.slodon.b2b2c.core.util.DateUtil.dateToString(enquiryEndTime, com.slodon.b2b2c.core.util.DateUtil.DATE_FORMAT_FIVE));
        memberEnquiryNewExample.setStatusNotEqual(EnquiryConst.ENQUIRY_DELETE);
        memberEnquiryNewExample.setItemTypeIn(itemTypeStr);
        memberEnquiryNewExample.setWebSite(webSite);
        enquiryOverviewVO.setEnquiryNewNum(memberEnquiryModel.getMemberEnquiryCount(memberEnquiryNewExample));

        //有效询盘数
        MemberEnquiryExample memberEnquiryValidExample = new MemberEnquiryExample();
        memberEnquiryValidExample.setStoreId(vendor.getStoreId());
        memberEnquiryValidExample.setValidStatus(EnquiryConst.ENQUIRY_VALID);
        memberEnquiryValidExample.setStatusNotEqual(EnquiryConst.ENQUIRY_DELETE);
        memberEnquiryValidExample.setItemTypeIn(itemTypeStr);
        memberEnquiryValidExample.setWebSite(webSite);
        enquiryOverviewVO.setValidInquiryNum(memberEnquiryModel.getMemberEnquiryCount(memberEnquiryValidExample));

        //待分配询盘数
        MemberEnquiryExample memberEnquiryToAssignExample = new MemberEnquiryExample();
        memberEnquiryToAssignExample.setStoreId(vendor.getStoreId());
        memberEnquiryToAssignExample.setStatusEqual(EnquiryConst.ENQUIRY_NEW);
        memberEnquiryToAssignExample.setItemTypeIn(itemTypeStr);
        memberEnquiryToAssignExample.setWebSite(webSite);
        enquiryOverviewVO.setEnquiryToBeAssignNum(memberEnquiryModel.getMemberEnquiryCount(memberEnquiryToAssignExample));

        return SldResponse.success(enquiryOverviewVO);
    }

    /**
     * 询盘趋势
     *
     * @param request
     * @return
     */
    @ApiImplicitParam(name = "webSite", value = "站点")
    @GetMapping("enquiryChangeTrend")
    public JsonResult<EnquiryTrendVO> getEnquiryTrend(HttpServletRequest request,@RequestParam(value = "webSite",required = false,defaultValue = "1") String webSite) throws Exception {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        EnquiryTrendVO vo = new EnquiryTrendVO();

        //获取当前时间30天之前的时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -30);
        Date startTime = DateUtil.beginOfDay(calendar.getTime());
        Date endTime = new Date();
        StatsTimeType timeType = StatsTimeType.checkTimeType(startTime, endTime, true);
        List<MemberEnquiry> list = memberEnquiryModel.getSellerEnquiryTrendList(timeType, startTime, endTime, webSite, vendor.getStoreId());
        List<EnquiryTrendVO.EnquiryNumVO> enquiryNumVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(memberEnquiry -> {
                EnquiryTrendVO.EnquiryNumVO enquiryNumVO = new EnquiryTrendVO.EnquiryNumVO();
                enquiryNumVO.setStatsTime(memberEnquiry.getEnquiryDay());
                enquiryNumVO.setViewNum(memberEnquiry.getEnquiryNum());
                enquiryNumVOList.add(enquiryNumVO);
            });
            vo.setEnquiryTrendList(enquiryNumVOList);
        }
        return SldResponse.success(vo);
    }
}
