package #(pkg).controller;

import com.eversec.common.AbstractBaseController;
import com.eversec.common.JBean;
import com.eversec.common.Domain;
import com.eversec.common.PageBean;
import #(pkg).domain.#(bean);
import #(pkg).service.#(bean)Service;
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.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
#if(hasDate)
import java.text.ParseException;
import java.text.SimpleDateFormat;
#end

/**
 * 类名称: #(bean)Controller<br>
 * 类描述: #(remark)<br>
 * 修改时间: #(ctime)<br>
 * @author #(author)
 */
@Api(tags = "#(remark)")
@RestController
public class #(bean)Controller extends AbstractBaseController {
    @Autowired
    private #(bean)Service #(beanFL)Service;

    @ApiOperation(value = "新增操作", notes = "新增")
    @RequestMapping(value = "/#(beanL)", method = RequestMethod.POST)
    public String save(@RequestBody #(bean) #(beanFL)) {
        JBean jBean = JBean.by(200, "成功", "");
        try {
            #(beanFL)Service.insert(#(beanFL));
        } catch (Exception e) {
            e.printStackTrace();
            jBean.set(-1, "操作失败");
        }
        return jBean.toJson();
    }

    @ApiOperation(value = "列表数据", notes = "统计列表数据")
    @RequestMapping(value = "/#(beanL)", method = RequestMethod.GET)
    @ResponseBody
    public String tolistPage(HttpServletRequest request) {
        #(bean) m = new #(bean)();

        //查询条件
        setCommonAttr(request, m);
        #if(hasDate)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        #end

#for(x : fieldList)
    #set(field=x.field
    ,field=field.substring(0, 1).toLowerCase() + field.substring(1)
    ,toUpperCaseFirst = field.substring(0, 1).toUpperCase() + field.substring(1)
    )
    #if((x.filter == "1" || x.filter == "like") && x.field != "tru" )
        #if(x.fieldType == "Long")
        if (!isEmpty(request.getParameter("#(field)"))) {
            m.set#(toUpperCaseFirst)(Long.parseLong(request.getParameter("#(field)")));
        }
        #elseif(x.fieldType == "Integer")
        if (!isEmpty(request.getParameter("#(field)"))) {
            m.set#(toUpperCaseFirst)(Integer.parseInt(request.getParameter("#(field)")));
        }
        #elseif(x.fieldType == "Date")
        if (!isEmpty(request.getParameter("#(field)"))) {
            try {
                m.set#(toUpperCaseFirst)(sdf.parse(request.getParameter("#(field)")));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        #else
        m.set#(toUpperCaseFirst)(request.getParameter("#(field)"));
        #end
    #elseif(x.filter == "2")
        try {
            if (!isEmpty(request.getParameter("start_#(field)"))) {
                m.setExtPara("start_#(field)", sdf.parse(request.getParameter("start_#(field)")));
            }
            if (!isEmpty(request.getParameter("end_#(field)"))) {
                m.setExtPara("end_#(field)", sdf.parse(request.getParameter("end_#(field)")));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    #end
#end

        PageBean pageBean = #(beanFL)Service.findPage(m);
        return JBean.by(200, "成功", pageBean).toJson();
    }

    @ApiOperation(value = "查询一条记录", notes = "查询一条记录")
    @RequestMapping(value = "/#(beanL)/findfirst", method = RequestMethod.GET)
    @ResponseBody
    public String findFirst(HttpServletRequest request) {
        #(bean) m = new #(bean)();

        //查询条件
        setCommonAttr(request, m);

        Domain domain = #(beanFL)Service.findFirst(m);
        return JBean.by(200, "成功", domain).toJson();
    }

    @ApiOperation(value = "删除")
    @RequestMapping(path = "/#(beanL)/{id}", method = RequestMethod.DELETE)
    public String delete(@ApiParam(value = "主键ID", required = true) @PathVariable final Long id) {

        #(beanFL)Service.deleteById(id);
        return JBean.by(200, "成功").toJson();
    }
}