package com.yihu.hos.qc.controller;

import com.google.common.base.Splitter;
import com.yihu.hos.qc.model.RuleModel;
import com.yihu.hos.qc.service.RuleService;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 质控规则管理
 *
 * @author CD
 * @vsrsion 1.0
 * Created at 2017/05/05.
 */
@RequestMapping("/rule")
@Controller
public class RuleController extends BaseController {

    @Resource(name = RuleService.BEAN_ID)
    private RuleService ruleService;


    /**
     * 质控规则管理界面
     *
     * @param model
     * @return
     */
    @RequestMapping("/initial")
    public String ruleInitial(Model model) {
        model.addAttribute("contentPage", "qc/rule");
        return "partView";
    }


    /**
     * 质控规则列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getRuleList")
    @ResponseBody
    public Result getAppList(HttpServletRequest request, String name, String valid) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("name", name);
            String page = StringUtils.isEmpty(request.getParameter("page")) ? "1" : request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows")) ? "10" : request.getParameter("rows");

            params.put("page", page);
            params.put("rows", rows);
            Result result = ruleService.getRuleList(params);
            return result;
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 质控规则修改页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editorRule")
    public String editoruleRule(Model model, String id) {
        try {
            RuleModel ruleModel = null;
            if (id != null) {
                ruleModel = ruleService.getRuleById(id);
            } else {
                ruleModel = new RuleModel();
            }
            model.addAttribute("model", ruleModel);
            model.addAttribute("contentPage", "/qc/editorRule");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    /**
     * 质控规则详情页
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/ruleDetail")
    public String ruleDetail(Model model, String id) {
        try {
            RuleModel ruleModel = null;
            if (id != null) {
                ruleModel = ruleService.getRuleById(id);
            } else {
                ruleModel = new RuleModel();
            }
            model.addAttribute("model", ruleModel);
            model.addAttribute("contentPage", "/rule/ruleDetail");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }


    /**
     * 新增质控规则s信息
     *
     * @param request
     * @return
     */
    @RequestMapping("addRule")
    @ResponseBody
    public Result addRule(HttpServletRequest request) {
        try {
            RuleModel obj = new RuleModel();
            BeanUtils.populate(obj, request.getParameterMap());
            return ruleService.addRule(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除质控规则信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/deleteRule")
    @ResponseBody
    public Result deleteRule(HttpServletRequest request) {

        try {
            String id = request.getParameter("id");

            ruleService.deleteRule(id);
            return Result.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败！");
        }
    }

    /**
     * 批量删除质控规则信息
     *
     * @param request
     * @return
     */
    @RequestMapping("/deleteRuleBatch")
    @ResponseBody
    public Result deleteRuleBatch(HttpServletRequest request) {
        try {
            String idList = request.getParameter("idList");
            List<String> ids = Splitter.on(",").trimResults().splitToList(idList);
            if (!CollectionUtils.isEmpty(ids)) {
                for (String id :
                        ids) {
                    ruleService.deleteRule(id);
                }
            }
            return Result.success("删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败！");
        }
    }

    /**
     * 修改质控规则信息
     */
    @RequestMapping("updateRule")
    @ResponseBody
    public Result updateRule(HttpServletRequest request) {
        try {
            RuleModel obj = new RuleModel();
            BeanUtils.populate(obj, request.getParameterMap());

            return ruleService.updateRule(obj);
        } catch (Exception ex) {
            ex.printStackTrace();
            return Result.error(ex.getMessage());
        }
    }
}
