package com.huiju.estateagents.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.entity.TaShareCount;
import com.huiju.estateagents.mapper.TaSharePersonFromMapper;
import com.huiju.estateagents.service.ITaShareCountService;
import com.huiju.estateagents.service.ITaSharePersonFromService;
import com.huiju.estateagents.service.impl.TaSharePersonFromServiceImpl;
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;
import java.time.LocalTime;

/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-02-03
 */
@RestController
@RequestMapping("/api")
public class TaShareCountController extends BaseController {

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

    @Autowired
    public ITaShareCountService iTaShareCountService;

    @Autowired
    private ITaSharePersonFromService iTaSharePersonFromService;

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/shareCountList",method= RequestMethod.GET)
    public ResponseBean taShareCountList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "personId") String personId,
                                         @RequestParam(value = "shareType", required = false) String shareType,
                                         @RequestParam(value ="shareTitle",required = false) String shareTitle, HttpServletRequest request
                                         ){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean = iTaShareCountService.selectList(pageNum, pageSize, getOrgId(request), personId, shareType, shareTitle);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/clickCountList",method= RequestMethod.GET)
    public ResponseBean clickCountList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                         @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                         @RequestParam(value = "personId") String personId,
                                         @RequestParam(value = "shareType", required = false) String shareType,
                                         @RequestParam(value ="shareTitle",required = false) String shareTitle, HttpServletRequest request
    ){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (StringUtils.isEmpty(personId)){
                return ResponseBean.success(null);
            }
            responseBean = iTaShareCountService.selectClickList(pageNum, pageSize, getOrgId(request), personId, shareType, shareTitle);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taShareCount 实体对象
     * @return
     */
    @RequestMapping(value="/taShareCount",method= RequestMethod.POST)
    public ResponseBean taShareCountAdd(@RequestBody TaShareCount taShareCount){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaShareCountService.save(taShareCount)){
                responseBean.addSuccess(taShareCount);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/taShareCount/{id}", method= RequestMethod.DELETE)
    public ResponseBean taShareCountDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTaShareCountService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taShareCount 实体对象
     * @return
     */
    @RequestMapping(value="/taShareCount/{id}",method= RequestMethod.PUT)
    public ResponseBean taShareCountUpdate(@PathVariable Integer id,
                                        @RequestBody TaShareCount taShareCount){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaShareCountService.updateById(taShareCount)){
                responseBean.addSuccess(taShareCount);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taShareCount/{id}",method= RequestMethod.GET)
    public ResponseBean taShareCountGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaShareCountService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taShareCountDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
