package com.yunzhi.marketing.xlk.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.xlk.dto.TrendDTO;
import com.yunzhi.marketing.xlk.entity.Trend;
import com.yunzhi.marketing.xlk.service.ITrendService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
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.RequestHeader;
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 org.springframework.web.bind.annotation.RestController;

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

/**
 * <p>
    * 项目动态表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/api")
@Api(value = "项目动态表", tags = "xlk-项目动态表")
public class TrendController extends BaseController {

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

    @Autowired
    public ITrendService iTrendService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "admin-查询项目动态列表", notes = "admin-查询项目动态列表")
    @RequestMapping(value="/{plat}/trend",method= RequestMethod.GET)
    public ResponseBean trendList(@PathVariable String plat,
                                  @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                  @RequestParam(value ="buildingId") String buildingId,
                                  @RequestHeader("authorization") String token,
                                  HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<Trend> pg = new Page<>(pageNum, pageSize);
            LambdaQueryWrapper<Trend> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Trend::getOrgId,getOrgId(request));
            queryWrapper.eq(Trend::getBuildingId,buildingId);
            queryWrapper.orderByDesc(Trend::getTrendTime);

            IPage<Trend> result = iTrendService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("trendList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param trendDTO 实体对象
     * @return
     */
    @ApiOperation(value = "admin-保存项目动态表", notes = "admin-保存项目动态表")
    @RequestMapping(value="/admin/trend",method= RequestMethod.POST)
    public ResponseBean trendAdd(@RequestBody TrendDTO trendDTO, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            Trend trend = new Trend();
            BeanUtils.copyProperties(trendDTO,trend);
            trend.setOrgId(getOrgId(request));
            trend.setCreateDate(LocalDateTime.now());
            if (iTrendService.save(trend)){
                responseBean.addSuccess(trend);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("trendAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-删除项目动态", notes = "admin-删除项目动态")
    @ResponseBody
    @RequestMapping(value="/admin/trend/{id}", method= RequestMethod.DELETE)
    public ResponseBean trendDelete(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTrendService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("trendDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param trendDTO 实体对象
     * @return
     */
    @ApiOperation(value = "admin-修改项目动态", notes = "admin-修改项目动态")
    @RequestMapping(value="/admin/trend/{id}",method= RequestMethod.PUT)
    public ResponseBean trendUpdate(@PathVariable String id,
                                    @RequestBody TrendDTO trendDTO, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            Trend trend = new Trend();
            BeanUtils.copyProperties(trendDTO,trend);
            trend.setTrendId(id);
            if (iTrendService.updateById(trend)){
                responseBean.addSuccess(trend);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("trendUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @ApiOperation(value = "admin-查询项目动态详情", notes = "admin-查询项目动态详情")
    @RequestMapping(value="/admin/trend/{id}",method= RequestMethod.GET)
    public ResponseBean trendGet(@PathVariable String id, @RequestHeader("authorization") String token, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTrendService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("trendDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
