package com.kiisoo.ic.store.controller;

import com.github.pagehelper.PageInfo;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.generalize.entity.PoiCustomerContactDataStat;
import com.kiisoo.ic.store.entity.StorePromotionDataDO;
import com.kiisoo.ic.store.service.StorePromotionDataService;
import com.kiisoo.ic.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author yunjie.jiang
 * @date 2020/4/10 10:49
 * @company kiisoo
 * @details 门店推广数据controller
 */
@Controller
@RequestMapping("/store/promotion/data")
@Slf4j
public class StorePromotionDataController extends BaseController {

    /**
     * 推广数据数据
     */
    @Autowired
    private StorePromotionDataService storePromotionDataService;

    /**
     * 查询门店推广数据列表
     * @param userId
     * @param storeId
     * @param regionId
     * @param sellerId
     * @param startDate
     * @param endDate
     * @param type 页面类型 "region".区域, "store".门店, "seller".导购
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listStorePromotionData(@RequestParam("userId") Long userId,
                                                     @RequestParam(value = "storeId", required = false) Long storeId,
                                                     @RequestParam(value = "regionId", required = false) Long regionId,
                                                     @RequestParam(value = "sellerId", required = false) Long sellerId,
                                                     @RequestParam(value = "startDate", required = false) Date startDate,
                                                     @RequestParam(value = "endDate", required = false) Date endDate,
                                                     @RequestParam("type") String type,
                                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                     @RequestParam(value = "pageSize", required = false, defaultValue = "15") int pageSize){
        try {
            PageInfo<StorePromotionDataDO> storePromotionDataDOS = storePromotionDataService.listStorePromotionData(userId, storeId,
                    regionId, sellerId, startDate, endDate, type, pageNum, pageSize);
            return data(storePromotionDataDOS);
        }catch (Exception e){
            log.error("查询门店推广数据列表",e);
            return fail();
        }
    }

    /**
     * 查询门店推广数据列表 手机端
     * @param userId
     * @param roleCode
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "list/wx",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listStorePromotionDataWx(@RequestParam("userId") Long userId,
                                                       @RequestParam("roleCode") String roleCode,
                                                     @RequestParam(value = "startDate", required = false) Date startDate,
                                                     @RequestParam(value = "endDate", required = false) Date endDate,
                                                     @RequestParam(value = "lastStartDate", required = false) Date lastStartDate,
                                                     @RequestParam(value = "lastEndDate", required = false) Date lastEndDate){
        try {
            StorePromotionDataDO storePromotionDataDOS = storePromotionDataService.listStorePromotionDataWx(userId, roleCode, startDate, endDate, lastStartDate,
                    lastEndDate);
            return data(storePromotionDataDOS);
        }catch (Exception e){
            log.error("查询门店推广数据列表",e);
            return fail();
        }
    }

    /**
     * 查询门店推广数据列表 手机端
     * @param response
     * @return
     */
    @RequestMapping(value = "distinct/customer/excel",method = RequestMethod.GET)
    public void distinctCustomer(HttpServletResponse response){
        try {
            storePromotionDataService.distinctCustomer(response);
        }catch (Exception e){
            log.error("查询门店推广数据列表",e);
        }
    }

    /**
     * 查询门店推广数据列表 手机端
     * @param userId
     * @param roleCode
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/behavior",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listUserBehaviorData(@RequestParam("userId") Long userId,
                                                     @RequestParam(value = "startDate",required = false) String startDate,
                                                     @RequestParam(value = "endDate",required = false) String endDate,
                                                     @RequestParam(value = "companyId",required = false) Long companyId,
                                                     @RequestParam(value = "shopId",required = false) Long shopId,
                                                     @RequestParam(value = "pageNum") Integer pageNum,
                                                     @RequestParam(value = "pageSize") Integer pageSize){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            PageInfo<PoiCustomerContactDataStat>  poiCustomerContactDataStatPageInfo = storePromotionDataService.listUserBehaviorData(userId,companyId, shopId,
                    StringUtils.isBlank(startDate) ? null : sdf.parse(startDate),
                    StringUtils.isBlank(endDate) ? null : sdf.parse(endDate),
                            pageNum,pageSize);
            return data(poiCustomerContactDataStatPageInfo);
        }catch (Exception e){
            log.error("查询门店推广数据列表",e);
            return fail();
        }
    }

    /**
     * 将绑定在账号上的推广数据绑定到店铺上
     * @param cpUserIds 企业微信账号  ',' 分割
     * @return
     */
    @RequestMapping(value = "/behavior/translate",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listUserBehaviorData(@RequestParam("cpUserIds") String cpUserIds){
        try {
            storePromotionDataService.translateDate(cpUserIds);
            return data(null);
        }catch (Exception e){
            log.error("查询门店推广数据列表",e);
            return fail();
        }
    }

    /**
     * 查询门店推广数据列表 手机端
     * @param userId
     * @param roleCode
     * @param startDate
     * @param endDate
     * @return
     */
    @RequestMapping(value = "/behavior/excel",method = RequestMethod.POST)
    @ResponseBody
    public void listUserBehaviorData(@RequestParam("userId") Long userId,
                                                   @RequestParam(value = "startDate",required = false) String startDate,
                                                   @RequestParam(value = "endDate",required = false) String endDate,
                                                   @RequestParam(value = "companyId",required = false) Long companyId,
                                                   @RequestParam(value = "shopId",required = false) Long shopId,
                                                   HttpServletResponse response){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            storePromotionDataService.downloadUserBehaviorDataExcel(userId,companyId, shopId,
                    StringUtils.isBlank(startDate) ? null : sdf.parse(startDate),
                    StringUtils.isBlank(endDate) ? null : sdf.parse(endDate),response);
        }catch (Exception e){
            log.error("查询门店推广数据列表",e);
        }
    }

}
