package com.huiju.estateagents.controller;

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.CommConstant;
import com.huiju.estateagents.entity.TpNews;
import com.huiju.estateagents.service.ITpNewsService;
import io.swagger.annotations.Api;
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.LocalDateTime;


/**
 * <p>
    * 资讯表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-07-25
 */
@RestController
@RequestMapping("/api")
@Api(value = "物业服务", tags = "物业服务")
public class TpNewsController extends BaseController {

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

    @Autowired
    ITpNewsService iTpNewsService;

    /**
     * 列表
     * @param pageNum
     * @param pageSize
     * @param newsName
     * @param client
     * @param request
     * @return
     */
    @GetMapping("/{client}/property-news")
    public ResponseBean newsList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                 @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "newsName", required = false) String newsName,
                                 @RequestParam(value = "newsTypeId", required = false) String newsTypeId,
                                 @RequestParam(value ="startCreateDate",required = false) String startCreateDate,
                                 @RequestParam(value ="endCreateDate",required = false) String endCreateDate,
                                 @PathVariable String client,
                                 HttpServletRequest request) {
        Integer orgId = getOrgId(request);

        Integer status = null;
        if ("wx".equals(client)) {
            status = CommConstant.IS_PUBLISH;
        }

        IPage<TpNews> page = new Page<>(pageNum, pageSize);
        IPage<TpNews> result = iTpNewsService.getListBy(page, orgId, newsName, status,newsTypeId,startCreateDate,endCreateDate);

        return ResponseBean.success(result);
    }

    /**
     * 获取详情
     * @param client
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/{client}/property-news/{id}")
    public ResponseBean getNewsByID(@PathVariable String client,
                                    @PathVariable Integer id,
                                    HttpServletRequest request) {
        Integer orgId = getOrgId(request);

        TpNews tpNews = iTpNewsService.getById(id);
        if (!orgId.equals(tpNews.getOrgId()) || CommConstant.STATUS_DELETE.equals(tpNews.getStatus())) {
            return ResponseBean.error("未找到对应内容", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        return ResponseBean.success(tpNews);
    }

    @PostMapping("/admin/property-news")
    public ResponseBean save(@RequestBody TpNews news, HttpServletRequest request){
        news.setOrgId(getOrgId(request));
        news.setCreateDate(LocalDateTime.now());

        if (news.getStatus() == null) {
            news.setStatus(CommConstant.NOT_PUBLISH);
        }

        if (!iTpNewsService.save(news)) {
            return ResponseBean.error("新增内容出错", ResponseBean.ERROR_UNAVAILABLE);
        }
        return ResponseBean.success(news);
    }

    @PutMapping(value="/admin/property-news/{id}")
    public ResponseBean taNewsUpdate(@PathVariable Integer id,
                                     @RequestBody TpNews tpNews,
                                     HttpServletRequest request){
        Integer orgId = getOrgId(request);

        tpNews.setNewsId(id);
        if (tpNews.getOrgId() == null) {
            tpNews.setOrgId(orgId);
        } else if (!orgId.equals(tpNews.getOrgId())) {
            return ResponseBean.error("您无权修改当前内容", ResponseBean.ERROR_UNAVAILABLE);
        }

        if (!iTpNewsService.updateById(tpNews)) {
            return ResponseBean.error("更新内容出错", ResponseBean.ERROR_UNAVAILABLE);
        }

        return ResponseBean.success(tpNews);
    }

    @DeleteMapping("/admin/property-news/{id}")
    public ResponseBean taNewsUpdate(@PathVariable Integer id,
                                     HttpServletRequest request){
        Integer orgId = getOrgId(request);

        TpNews tpNews = iTpNewsService.getById(id);
        if (!orgId.equals(tpNews.getOrgId())) {
            return ResponseBean.error("您无权删除当前内容", ResponseBean.ERROR_UNAVAILABLE);
        }

        tpNews.setStatus(CommConstant.STATUS_DELETE);
        if (!iTpNewsService.updateById(tpNews)) {
            return ResponseBean.error("删除内容出错", ResponseBean.ERROR_UNAVAILABLE);
        }

        return ResponseBean.success("删除成功");
    }
}
