package com.runshi.autosite.controller.cms;
import com.runshi.autosite.dao.cms.CmsLinkInfoDao;
import com.runshi.autosite.po.PageParameter;
import com.runshi.autosite.po.ResponseResult;
import com.runshi.autosite.po.CmsLinkInfo;
import com.runshi.autosite.utils.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* @author cli
*/
@RestController
@RequestMapping("/cmslinkinfo")
@Api(tags ="链接_链接信息表api")
@Transactional
public class CmsLinkInfoController {
/**
* 默认主键
*/
private static final StringPRA_KEY ="id";
/**
* 业务名称
*/
private static final StringBIZ_NAME ="链接_链接信息表";;
/**
* 业务对象类
*/
private static final StringBIZ_PO ="CmsLinkInfo";
/**
* 业务对象类名称
*/
private static final StringBIZ_PO_NAME ="CmsLinkInfo";
/**
* 返回结果
*/
private ResponseResultresult =new ResponseResult();
@Autowired
private CmsLinkInfoDaodao;
@ApiOperation(value ="获取" +BIZ_NAME +"列表", notes ="全部列表")
@RequestMapping(value = {""}, method = RequestMethod.GET)
public ResponseResult list() {
try {
result = CommonUtil.setResult("0","查询成功",dao.findAll());
}catch (Exception ex) {
result = CommonUtil.setResult("1", ex.getMessage(),null);
}
return result;
}
@ApiOperation(value ="获取" +BIZ_NAME +"详细信息", notes ="根据url的id来获取" +BIZ_NAME +"详细信息")
@ApiImplicitParam(name ="id", value =BIZ_NAME +"ID", required =true, dataType ="int", paramType ="path")
@RequestMapping(value ="/{id}", method = RequestMethod.GET)
public ResponseResult info(@PathVariable int id) {
try {
result = CommonUtil.setResult("0","查询成功",dao.findById(id));
}catch (Exception ex) {
result = CommonUtil.setResult("1", ex.getMessage(),null);
}
return result;
}
@ApiOperation(value ="获取" +BIZ_NAME +"分页列表", notes ="page:1,size:10,dir:asc/desc,sort:对象字段,parameters:{}")
@ApiImplicitParams({
@ApiImplicitParam(name ="pageParameter", dataType ="PageParameter", value ="自定义条件")
})
@RequestMapping(value ="/page", method = RequestMethod.POST)
public ResponseResult pagelist(@RequestBody PageParameter pageParameter) {
try {
result = CommonUtil.setResult("0",
"查询成功",
pager(pageParameter.getPage(), pageParameter.getSize(),
"".equals(pageParameter.getDir()) ||"asc".equals(pageParameter.getDir()) ? Sort.Direction.ASC : Sort.Direction.DESC,
"".equals(pageParameter.getSort()) ?PRA_KEY : pageParameter.getSort(), pageParameter.getParameters()));
}catch (Exception ex) {
result = CommonUtil.setResult("1", ex.getMessage(),null);
}
return result;
}
@ApiOperation(value ="删除" +BIZ_NAME, notes ="根据url的id删除对象")
@ApiImplicitParam(name ="id", value =BIZ_NAME +"ID", required =true, dataType ="int", paramType ="path")
@RequestMapping(value ="/{id}", method = RequestMethod.DELETE)
public ResponseResult delete(@PathVariable int id) {
try {
dao.deleteById(id);
result = CommonUtil.setResult("0","删除成功","");
}catch (Exception ex) {
result = CommonUtil.setResult("1", ex.getMessage(),null);
}
return result;
}
@ApiOperation(value ="保存" +BIZ_NAME +"信息", notes ="根据" +BIZ_PO_NAME +"对象保存链接_链接信息表")
@ApiImplicitParam(name =BIZ_PO_NAME, value =BIZ_NAME +"详细实体" +BIZ_PO_NAME, required =true, dataType =BIZ_PO, paramType ="path")
@RequestMapping(value ="/save", method = RequestMethod.POST)
public ResponseResult saves(CmsLinkInfo cmsLinkInfo) {
return save(cmsLinkInfo);
}
/**
* 保存
*/
private ResponseResult save(CmsLinkInfo cmsLinkInfo) {
String code, msg;
int pra;
try {
if (cmsLinkInfo.getId() !=null) {
CmsLinkInfo searchCmsLinkInfo =dao.findById(cmsLinkInfo.getId()).get();
code ="0";
msg ="更新" +BIZ_NAME +"成功";
pra = cmsLinkInfo.getId();
result = CommonUtil.setResult(code, msg,dao.save(CommonUtil.mergeObject(cmsLinkInfo, searchCmsLinkInfo)),pra);
}else {
CmsLinkInfo data =dao.saveAndFlush(cmsLinkInfo);
code ="0";
msg ="保存" +BIZ_NAME +"成功";
pra = data.getId();
result = CommonUtil.setResult(code, msg, data,pra);
}
}catch (Exception ex) {
result = CommonUtil.setResult("1", ex.getMessage(),null);
}
return result;
}
/**
* 分页方法
*
* @param page 当前页
* @param size 每页记录数
* @param dir 排序
* @param sort 排序字段
* @param parameters 自定义参数
* @return 分页数据
*/
private Page pager(int page,int size, Sort.Direction dir, String sort, HashMap parameters) {
Pageable pageable = PageRequest.of(page -1, size, dir, sort);
Specification specification = getSearch(parameters);
return dao.findAll(specification, pageable);
}
/**
* 自定义查询条件
*
* @param parameters 自定义条件
* @return Specification
*/
private Specification getSearch(HashMap parameters) {
return (root, criteriaQuery, criteriaBuilder) -> {
List predicate =new ArrayList<>();
// 查询条件
Predicate[] pre =new Predicate[predicate.size()];
return criteriaQuery.where(predicate.toArray(pre)).getRestriction();
};
}
}