package ${package.Controller};

import ${package.Entity}.${entity};
import ${package.Service}.${table.serviceName};
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.unis.crk.utils.ResultUtil;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import ${package.Entity}.vo.${entity}Vo;
#if(${swagger2})
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.hutool.json.JSONObject;
#end
#if(${restControllerStyle})
#else
import org.springframework.stereotype.Controller;
#end
#if(${superControllerClassPackage})
import ${superControllerClassPackage};
#end

/**
 * <p>
 * $!{table.comment} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@CrossOrigin
#if(${swagger2})
@Api(value = "$!{table.comment}",tags = {"$!{table.comment}"})
#end
#if(${restControllerStyle})
@RestController
#else
@Controller
#end
@RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end#if(${controllerMappingHyphenStyle})${controllerMappingHyphen}#else${table.entityPath}#end")
#if(${kotlin})
class ${table.controllerName}#if(${superControllerClass}) : ${superControllerClass}()#end

#else
#if(${superControllerClass})
public class ${table.controllerName} extends ${superControllerClass} {
#else
public class ${table.controllerName} {
#end

    private final static Logger LOGGER = LoggerFactory.getLogger(${table.controllerName}.class);

    #set($lowerCaseServiceName=${table.serviceName})
    #set($lowerCaseServiceName=$lowerCaseServiceName.substring(1))
    #set($lowerCaseServiceName=$lowerCaseServiceName.substring(0,1).toLowerCase()+$lowerCaseServiceName.substring(1))

    @Autowired
    private ${table.serviceName} ${lowerCaseServiceName};


    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    #if(${swagger2})
    @ApiOperation(value="查询", notes="根据ID查询数据")
    #end
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<${entity}> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        data.success(this.${lowerCaseServiceName}.getById(id));
        return data;
    }

    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody ${entity}Vo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.${lowerCaseServiceName}.findPageVo(pageQuery));
        return data;
    }

    /**
     * 新增一条数据
     * @param entity
     * @return
     */
    #if(${swagger2})
    @ApiOperation(value="新增", notes="新增一条数据")
    #end
    @PostMapping("add")
    public ResultUtil add(@RequestBody ${entity} entity){
        LOGGER.info("入参为:{}", entity);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(entity)){
            data.failed("所接收对象为null");
            return data;
        }
        JSONObject ret  = this.${lowerCaseServiceName}.saveT(entity);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }

        return data;
    }


    /**
     * 修改一条数据
     * @param entity
     * @return
     */
    #if(${swagger2})
    @ApiOperation(value="修改", notes="修改一条数据")
    #end
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  ${entity} entity){
        LOGGER.info("入参为:{}", entity);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(entity)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret  = this.${lowerCaseServiceName}.update(entity);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }

        return data;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    #if(${swagger2})
    @ApiOperation(value="删除", notes="根据ID删除数据")
    #end
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        JSONObject ret  = this.${lowerCaseServiceName}.delete(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }


}

#end