package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.search.ReturnVisitSearch;
import com.yonge.cooleshow.biz.dal.entity.ReturnVisit;
import com.yonge.cooleshow.biz.dal.service.ReturnVisitService;
import com.yonge.cooleshow.biz.dal.vo.ReturnVisitVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.base.page.PageInfo;
import com.yonge.toolset.base.util.StringUtil;
import com.yonge.toolset.mybatis.support.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

@RestController
@RequestMapping("${app-config.url.admin:}/returnVisit")
@Api(value = "回访表", tags = "回访表")
public class ReturnVisitController extends BaseController {

    @Autowired
    private ReturnVisitService returnVisitService;

	@Autowired
	private SysUserFeignService sysUserFeignService;

	/**
     * 查询单条
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入id")
    public HttpResponseResult<ReturnVisitVo> detail(@PathVariable("id") Long id) {
    	return succeed(returnVisitService.detail(id));
	}
    
    /**
     * 查询分页
     */
    @PostMapping("/page")
    @ApiOperation(value = "查询分页", notes = "传入returnVisitSearch")
    public HttpResponseResult<PageInfo<ReturnVisitVo>> page(@RequestBody ReturnVisitSearch query) {
		IPage<ReturnVisitVo> pages = returnVisitService.selectPage(PageUtil.getPage(query), query);
        return succeed(PageUtil.pageInfo(pages));
	}
    
    /**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入returnVisit")
	public HttpResponseResult save(@Valid @RequestBody ReturnVisit returnVisit) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		returnVisit.setCreateBy(user.getId());
		returnVisit.setCreateTime(new Date());

    	return status(returnVisitService.save(returnVisit));
	}
    
    /**
	 * 修改
	 */
	// @PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入returnVisit")
	public HttpResponseResult update(@Valid @RequestBody ReturnVisit returnVisit) {
        return status(returnVisitService.updateById(returnVisit));
	}


 	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public HttpResponseResult remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        if (StringUtil.isEmpty(ids)) {
			return failed("参数不能为空");
		}
		return status(returnVisitService.removeByIds(StringUtil.toLongList(ids)));
	}
}