package com.yunzhi.marketing.controller;


import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.service.ITaShareService;
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.HttpServletRequest;

/**
 * <p>
 * 用户访问表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@RestController
@RequestMapping("/api")
public class TaShareController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaShareController.class);

    @Autowired
    ITaShareService taShareService;

    @PostMapping("/wx/{typeOf}/share/{id}")
    public ResponseBean save(@PathVariable String typeOf, @PathVariable String id, HttpServletRequest request) {
        String openid = getOpenId(request);
        return taShareService.saveData(openid, typeOf, id);
    }

    @GetMapping("/wx/{typeOf}/share/{id}")
    public ResponseBean hotList(@PathVariable String typeOf, @PathVariable String id, Integer pageNumber, Integer pageSize, HttpServletRequest request) {
        if (null == pageNumber) pageNumber = 1;
        if (null == pageSize) pageSize = 10;

        return taShareService.getListOf(id, typeOf, pageNumber, pageSize);
    }

    /**
     * 确认分享, 送积分
     *
     * @param typeOf
     * @param id
     * @param request
     * @return
     */
    @PutMapping("/wx/{typeOf}/share/{id}")
    public ResponseBean makeSure(@PathVariable String typeOf, @PathVariable String id, HttpServletRequest request) {
        String openid = getOpenId(request);
        Integer orgId = getOrgId(request);
        taShareService.makeSure(openid, typeOf, id, orgId);
        return ResponseBean.success("");
    }

    @RequestMapping(value = "/admin/taHouseShare", method = RequestMethod.GET)
    public ResponseBean taHouseShareList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "shareType", required = false) String shareType,
                                         @RequestParam(value = "personType", required = false) String personType,
                                         @RequestParam(value = "phone", required = false) String phone,
                                         @RequestParam(value = "salesBatchId") Integer salesBatchId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (salesBatchId == null) {
                return ResponseBean.success("");
            }
            responseBean = taShareService.selectHouseShareList(pageNum, pageSize, getOrgId(request), personType, shareType, phone, salesBatchId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("taShareCountList -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    @RequestMapping(value = "/admin/taLiveShare", method = RequestMethod.GET)
    public ResponseBean listLiveShare(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                      @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                      @RequestParam(value = "shareType", required = false) String shareType,
                                      @RequestParam(value = "personType", required = false) String personType,
                                      @RequestParam(value = "phone", required = false) String phone,
                                      @RequestParam(value = "liveActivityId") Integer liveActivityId, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            if (liveActivityId == null) {
                return ResponseBean.success("");
            }
            responseBean = taShareService.listLiveShare(pageNum, pageSize, getOrgId(request), personType, shareType, phone, liveActivityId);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("listLiveShare -=- {}", e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
