package com.ym.mec.cms.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ym.mec.cms.controller.queryinfo.NewsInformationQueryInfo;
import com.ym.mec.cms.dal.dao.SysNewsInformationDao;
import com.ym.mec.cms.dal.entity.SysNewsInformation;
import com.ym.mec.cms.service.SysNewsInformationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.util.collection.MapUtil;

@RestController
@RequestMapping("news")
@Api(tags = "资讯服务")
public class NewsController extends BaseController {

	@Autowired
	private SysNewsInformationService sysNewsInformationService;
	
	@Autowired
	private SysNewsInformationDao sysNewsInformationDao;
	
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation("资讯列表分页查询")
	@GetMapping(value = "/list")
	public Object getList(NewsInformationQueryInfo queryInfo) {
		if(queryInfo.getTenantId() == null){
			queryInfo.setTenantId(1);
		}
		
		Map<String, Object> params = new HashMap<String, Object>();
		MapUtil.populateMap(params, queryInfo);
		int count = sysNewsInformationDao.queryCount(params);
		if(count == 0){
			queryInfo.setMemo(null);
		}
		return succeed(sysNewsInformationService.queryPage(queryInfo));
	}

	@ApiOperation("资讯列表分页查询")
	@GetMapping(value = "/homeList")
	public Object getHomeList(NewsInformationQueryInfo queryInfo) {
		queryInfo.setRows(10);
		SysUser user = sysUserFeignService.queryUserInfo();
		return succeed(sysNewsInformationService.getHomeList(user, queryInfo));
	}

	@ApiOperation("查询资讯详情")
	@ApiImplicitParam(name = "id", value = "资讯ID编号", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "/query")
	public Object query(Long id) {

		return succeed(sysNewsInformationService.get(id));
	}

	@ApiOperation("新增资讯")
	@PostMapping(value = "/add", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object add(SysNewsInformation newsInfo) {
		return succeed(sysNewsInformationService.insert(newsInfo));
	}

	@ApiOperation("更新资讯")
	@PostMapping(value = "/update", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object update(SysNewsInformation newsInfo) {
		Date date = new Date();
		newsInfo.setUpdateTime(date);

		return succeed(sysNewsInformationService.update(newsInfo));
	}

	@ApiOperation("删除")
	@PostMapping(value = "/del/{id}", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public Object add(@PathVariable("id") Long id) {
		return succeed(sysNewsInformationService.deleteWithLogical(id));
	}

}
