package com.weavi.controller;

import com.weavi.entity.Robot;
import com.weavi.repository.RobotDao;
import com.weavi.util.StringUtil;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/weavi")
public class RobotController {
    @Autowired
    private RobotDao robotDao;

    @ResponseBody
    @RequestMapping("/delete")
    public Map<String,Object> delete(@RequestParam(value="id") String id){
        Map<String,Object> result = new HashMap<String,Object>();
        String[] msg = id.split(",");
        for(int i=0;i < msg.length;i++){
            robotDao.delete(Integer.parseInt(msg[i]));
        }
        result.put("success",true);
        return result;
    }

    @ResponseBody
    @RequestMapping("/save")
    public Map<String,Object> edit(Robot robot){
        Map<String,Object> result = new HashMap<String,Object>();
        robotDao.save(robot);
        result.put("success",true);
        return result;
    }

    @ResponseBody
    @RequestMapping("/list")
    public Map<String, Object> list(final Robot robot,
                                    @RequestParam(value = "page", required = false) Integer page,
                                    @RequestParam(value = "rows", required = false) Integer rows) {

        Pageable pageable = new PageRequest(page-1, rows, Sort.Direction.ASC, "id");

        Page<Robot> pageRobot = robotDao.findAll(new Specification<Robot>() {

            @Override
            public Predicate toPredicate(Root<Robot> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Predicate predicate = cb.conjunction();
                if (robot != null) {
                    if (StringUtil.isNotEmpty(robot.getName())) {
                        predicate.getExpressions().add(cb.like(root.<String>get("name"), "%" + robot.getName().trim() + "%"));
                    }
                    if (StringUtil.isNotEmpty(robot.getPassword())) {
                        predicate.getExpressions().add(cb.like(root.<String>get("password"), "%" + robot.getPassword().trim() + "%"));
                    }
                }
                return predicate;
            }

        }, pageable);
        // 获取rows
        List<Robot> list = pageRobot.getContent();
        // 获取count
        Long count = robotDao.count(new Specification<Robot>() {

            @Override
            public Predicate toPredicate(Root<Robot> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
               Predicate predicate = cb.conjunction();
               if (robot!= null) {
                   if (StringUtil.isNotEmpty(robot.getName())) {
                        predicate.getExpressions().add(cb.like(root.<String>get("name"), "%" + robot.getName().trim() + "%"));
                    }
                  if (StringUtil.isNotEmpty(robot.getPassword())) {
                        predicate.getExpressions().add(cb.like(root.<String>get("password"), "%" + robot.getPassword().trim() + "%"));
                    }
                }
                return predicate;
           }

        });
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("rows", list);
        resultMap.put("total", count);
        resultMap.put("success", true);
        return resultMap;
    }

    @RequestMapping("/robot")
    public String crud() {
        return "robot";
    }
}
