package com.yunzhi.marketing.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.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.yunzhi.marketing.service.ITaMiniappMessagePersonService;
import com.yunzhi.marketing.entity.TaMiniappMessagePerson;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
    * 消息订阅人员 前端控制器
    * </p>
 *
 * @author yansen
 * @since 2021-08-13
 */

@Api(tags = "消息订阅人员")
@RestController
@RequestMapping("/api")
public class TaMiniappMessagePersonController extends BaseController {

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

    @Autowired
    public ITaMiniappMessagePersonService iTaMiniappMessagePersonService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/wx/miniapp-message",method= RequestMethod.GET)
    @ApiOperation(value="列表", notes = "列表", httpMethod = "GET", response = ResponseBean.class)
    public ResponseBean taMiniappMessagePersonList(@ApiParam("页码") @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                                   @ApiParam("单页数据量") @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                                   @ApiParam("人员") @RequestParam(value ="personId", required = false) String personId,
                                                   @ApiParam("楼盘") @RequestParam(value ="buildingId", required = false) String buildingId,
                                                   @ApiParam("消息类型") @RequestParam(value ="messageType", required = false) String messageType,
                                                   HttpServletRequest request) throws Exception{
        Integer orgId = getOrgId(request);

        IPage<TaMiniappMessagePerson> pg = new Page<>(pageNum, pageSize);
        QueryWrapper<TaMiniappMessagePerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId);
        queryWrapper.eq(!StringUtils.isEmpty(personId), "person_id", personId);
        queryWrapper.eq(!StringUtils.isEmpty(buildingId), "building_id", buildingId);
        queryWrapper.eq(!StringUtils.isEmpty(messageType), "message_type", messageType);
        queryWrapper.orderByDesc("create_date");

        IPage<TaMiniappMessagePerson> result = iTaMiniappMessagePersonService.page(pg, queryWrapper);
        return ResponseBean.success(result);
    }

    /**
     * 保存对象
     * @param taMiniappMessagePerson 实体对象
     * @return
     */
    @RequestMapping(value="/wx/miniapp-message",method= RequestMethod.POST)
    @ApiOperation(value="保存", notes = "保存", httpMethod = "POST", response = ResponseBean.class)
    public ResponseBean taMiniappMessagePersonAdd(@ApiParam("保存内容") @RequestBody TaMiniappMessagePerson taMiniappMessagePerson,
                                                  HttpServletRequest request) throws Exception {
        Integer orgId = getOrgId(request);
        String personId = getPersonId(request);
        String buildingId = taMiniappMessagePerson.getBuildingId();
        String messageType = taMiniappMessagePerson.getMessageType();

        if (StringUtils.isEmpty(messageType)) {
            return ResponseBean.error("请设置订阅消息类型", ResponseBean.ERROR_UNAVAILABLE);
        }

        QueryWrapper<TaMiniappMessagePerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("org_id", orgId);
        queryWrapper.eq("person_id", personId);
        queryWrapper.eq(StringUtils.isEmpty(buildingId),"building_id", buildingId);
        queryWrapper.eq("message_type", messageType);

        int count = iTaMiniappMessagePersonService.count(queryWrapper);
        if (count > 0) {
            // 如果已经订阅过改消息类型, 则不处理
            return ResponseBean.success("");
        }

        taMiniappMessagePerson.setOrgId(orgId);
        taMiniappMessagePerson.setPersonId(personId);
        taMiniappMessagePerson.setCreateDate(LocalDateTime.now());

        if (iTaMiniappMessagePersonService.save(taMiniappMessagePerson)){
            return ResponseBean.success("");
        }else {
            return ResponseBean.error("订阅失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
        }
    }

//    /**
//     * 根据id删除对象
//     * @param id  实体ID
//     */
//    @RequestMapping(value="/taMiniappMessagePerson/{id}", method= RequestMethod.DELETE)
//    @ApiOperation(value="删除", notes = "删除", httpMethod = "DELETE", response = ResponseBean.class)
//    public ResponseBean taMiniappMessagePersonDelete(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
//        if(iTaMiniappMessagePersonService.removeById(id)){
//            return ResponseBean.success("success");
//        }else {
//            return ResponseBean.error("删除失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
//        }
//    }
//
//    /**
//     * 修改对象
//     * @param id  实体ID
//     * @param taMiniappMessagePerson 实体对象
//     * @return
//     */
//    @RequestMapping(value="/taMiniappMessagePerson/{id}",method= RequestMethod.PUT)
//    @ApiOperation(value="更新", notes = "更新", httpMethod = "PUT", response = ResponseBean.class)
//    public ResponseBean taMiniappMessagePersonUpdate(@ApiParam("对象ID") @PathVariable Integer id,
//                                        @ApiParam("更新内容") @RequestBody TaMiniappMessagePerson taMiniappMessagePerson) throws Exception{
//
//        if (iTaMiniappMessagePersonService.updateById(taMiniappMessagePerson)){
//            return ResponseBean.success(iTaMiniappMessagePersonService.getById(id));
//        }else {
//            return ResponseBean.error("修改失败, 请重试", ResponseBean.ERROR_UNAVAILABLE);
//        }
//    }
//
//    /**
//     * 根据id查询对象
//     * @param id  实体ID
//     */
//    @RequestMapping(value="/taMiniappMessagePerson/{id}",method= RequestMethod.GET)
//    @ApiOperation(value="详情", notes = "详情", httpMethod = "GET", response = ResponseBean.class)
//    public ResponseBean taMiniappMessagePersonGet(@ApiParam("对象ID") @PathVariable Integer id) throws Exception{
//        return ResponseBean.success(iTaMiniappMessagePersonService.getById(id));
//    }
}
