package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.Partner;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.PartnerSearchModel;
import com.lightinit.hsdataplatform.service.admin.IPartnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

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

@Controller
@RequestMapping("/admin/partner")
public class PartnerController extends BaseController {
    @Autowired
    private IPartnerService iPartnerService;

    /**
     * 跳转页面
     * @return
     */
    @RequestMapping("index.htm")
    public ModelAndView index () {
        ModelAndView modelAndView = new ModelAndView("admin/partner/index");
        return modelAndView;
    }

    /**
     * 查询列表
     * @param partnerSearchModel
     * @param pageNo
     * @return
     */
    @RequestMapping("list.action")
    public ModelAndView list(PartnerSearchModel partnerSearchModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/partner/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<Partner> outputModel = iPartnerService.queryList(partnerSearchModel, pager);
        pager.setTotal(iPartnerService.queryCount(partnerSearchModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    /**
     * 添加方法
     * @param inputModel
     * @return
     */
    @RequestMapping(value = "add.action")
    @ResponseBody
    public ResultState doAdd(@Valid Partner inputModel, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        ResultState resultState = iPartnerService.add(inputModel);
        return resultState;
    }

    /**
     * 跳转添加页面
     * @return
     */
    @RequestMapping(value = "add.htm")
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("admin/partner/add");
        return modelAndView;
    }

    /**
     * 删除方法
     * @param id
     * @return
     */
    @RequestMapping(value = "del.action/{id}")
    @ResponseBody
    public ResultState delete(@PathVariable long id){
        ResultState<Long> resultState = iPartnerService.delete(id);
        return resultState;
    }

    /**
     * 跳转修改页
     * @param id
     * @return
     */
    @RequestMapping(value = "update.htm/{id}")
    public ModelAndView edit(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/partner/edit");
        modelAndView.addObject("busiModel", iPartnerService.queryOne(id));
        return modelAndView;
    }

    /**
     * 修改
     * @param inputModel
     * @return
     */
    @RequestMapping(value = "doEdit.action")
    @ResponseBody
    public ResultState doEdit(@Valid Partner inputModel,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return getAllFieldInvalidResultState(inputModel, bindingResult);
        }
        ResultState resultState = iPartnerService.edit(inputModel);
        return resultState;
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @RequestMapping(value = "toDetail.htm/{id}")
    @ResponseBody
    public ModelAndView toDetail(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/partner/detail");
        modelAndView.addObject("busiModel", iPartnerService.detail(id));
        return modelAndView;
    }
}
