package com.bofeng.wx.controller;

import com.bofeng.entity.QzTask;
import com.bofeng.entity.QzTaskReply;
import com.bofeng.entity.SysUptownHouse;
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.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.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;

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

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

        List<QzTask> taskList = qzTaskService.selectAll();
        List<SysUptownHouse> rbList = rbService.selectAll();
        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, 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);

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

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

        Integer success = qzTaskReplyService.insertQzTaskReply(qzTaskReply);

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

    @PostMapping("/yeweihui/qiuzhu/list.json")
    public Model queryByHouseNumber(@Pd(name = "houseNumber") String houseNumber, @Pd(name = "status") Integer 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(String str) {
        String[] split = str.split("&");
        List<SysUptownHouse> rbList = rbService.selectXq(split[0],split[1]);

        return Model.newSuccess(rbList);
    }
}
