package com.bofeng.wx.controller;

import com.bofeng.entity.QzTask;
import com.bofeng.entity.QzTaskReply;
import com.bofeng.entity.SysUptownHouse;
import com.bofeng.entity.sysUptownUnit;
import com.bofeng.service.HomeService;
import com.bofeng.entity.*;
import com.bofeng.service.QzTaskReplyService;
import com.bofeng.service.QzTaskService;
import com.bofeng.service.RbService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Maps;
import com.yvan.Model;
import com.yvan.ModelOps;
import com.yvan.mvc.Pd;
import com.yvan.platform.JsonWapper;
import com.yvan.platform.YvanUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
public class YeWeiHuiController {

    @Autowired
    private QzTaskService qzTaskService;
    @Autowired
    private RbService rbService;

    @Autowired
    private QzTaskReplyService qzTaskReplyService;

    @Autowired
    private HomeService homeService;

    @GetMapping("/yeweihui/home.html")
    public ModelAndView yeweihui(ModelMap model,@RequestParam(value = "userId", required = false,defaultValue = "0") Long userId) {

        Map<String, Object> queryParam = Maps.newLinkedHashMap();
        queryParam.put("userId", "12345677");
        queryParam.put("statistics", "M");

//        List<QzTask> taskList = qzTaskService.selectAll(2L);
        List<sysUptownUnit> rbList = rbService.selectAll(1225331049509187586L);
//        model.put("taskList", YvanUtil.toJsonPretty(taskList));
        model.put("rbList", YvanUtil.toJsonPretty(rbList));

        return new ModelAndView("/yeweihui/home.ftl", model);
    }

    @GetMapping("/yeweihui/qiuzhuDetail.html")
    public ModelAndView qiuzhudetail(@Pd(name = "taskId") Long taskId,@Pd(name = "userId") Long userId, ModelMap model) throws JsonProcessingException {

        QzTask task = qzTaskService.queryByTaskId(taskId);
//        A业委会，B居委会，C物业，D志愿者
        StringBuffer target = new StringBuffer();
        if (task.getTaskTarget().equals("A")) {
            target.append("业委会");
        }
        else if (task.getTaskTarget().equals("B")) {
            if (target.length() > 0) {
                target.append("、居委会");
            }
            else {
                target.append("居委会");
            }
        }
        else if (task.getTaskTarget().equals("C")) {
            if (target.length() > 0) {
                target.append("、物业");
            }
            else {
                target.append("物业");
            }
        }
        else if (task.getTaskTarget().equals("D")) {
            if (target.length() > 0) {
                target.append("、志愿者");
            }
            else {
                target.append("志愿者");
            }
        }

        task.setTaskTarget(target.toString());

        model.put("taskJson", new JsonWapper(task));
        model.put("task", task);
        model.put("userId", "\""+userId+"\"");

        return new ModelAndView("/yeweihui/qiuzhuDetail.ftl", model);
    }

    @PostMapping("/yeweihui/qiuzhu/queryTasksByStatus.json")
    public Model queryTasksByStatus(@Pd(name = "status") Integer status) {

        List<QzTask> taskList = qzTaskService.queryQzTaskByStatus(status);

        return Model.newSuccess(taskList);
    }

    @PostMapping("/yeweihui/qiuzhu/reply/add.json")
    public ModelOps replyQiuzhuInsert(QzTaskReply qzTaskReply) {

        Integer success = qzTaskReplyService.insertQzTaskReply(qzTaskReply);

        if (success > 0) {
            return ModelOps.newSuccess();
        }
        else {
            return ModelOps.newFail("操作失败");
        }
    }

    @PostMapping("/yeweihui/qiuzhu/reply/querybytaskid.json")
    public Model replyQiuzhuQueryByTaskId(@Pd(name = "taskId") Long taskId) {

        List<QzTaskReply> reply = qzTaskReplyService.queryQzTaskReplyByTaskId(taskId);

        return Model.newSuccess(reply);
    }

    @PostMapping("/yeweihui/qiuzhu/list.json")
    public Model queryByHouseNumber(String houseNumber, @Pd(name = "status") Integer status) {

        if (houseNumber.length() <= 0) {
            return Model.newSuccess(qzTaskService.queryQzTaskByStatus(status));
        }

        List<QzTask> list = qzTaskService.queryByHouseNumber(houseNumber, status);

        return Model.newSuccess(list);
    }

    @GetMapping("/yeweihui/ribao.html")
    public ModelAndView yeweihuiRibao(ModelMap model) {

//        List<QzTask> taskList = qzTaskService.selectAll();
//
//        model.put("taskList", YvanUtil.toJsonPretty(taskList));

        return new ModelAndView("/yeweihui/ribao.ftl", model);
    }

    @GetMapping("/yeweihui/ribaoDy")
    public Model<List<SysUptownHouse>> yeweihuiRibaoDy(Long str) {
//        String[] split = str.split("&");
        List<SysUptownHouse> rbList = rbService.selectXq(str);

        return Model.newSuccess(rbList);
    }

    @GetMapping("/yeweihui/ribaoXq.html")
    public ModelAndView yeweihuiRibaoYcXq(ModelMap model) {
        Map<String, Object> queryParam = Maps.newLinkedHashMap();
        queryParam.put("userId", "12345677");
        queryParam.put("statistics", "M");
        return new ModelAndView("/yeweihui/ribaoXq.ftl", model);
    }

    @GetMapping("/yeweihui/ribaoDyYcXq")
    public Model<List<MsSuspected>> yeweihuiRibaoYcXq(Long houseId) {
        List<MsSuspected> rbList = rbService.selectYcXq(houseId);
        return Model.newSuccess(rbList);
    }
}
