package com.sifang.modules.sys.web;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerService;
import com.sifang.modules.platform.service.AddressService;
import com.sifang.modules.platform.utils.AddressUtils;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.CrmPlatformAnnouncement;
import com.sifang.modules.sys.service.CrmPlatformAnnouncementService;
import com.sifang.modules.sys.util.*;
import com.sifang.modules.sys.web.form.NotificationForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("${adminPath}/notification/")
public class CrmPlatformAnnouncementController extends DjxBaseController {

    @Autowired
    CrmPlatformAnnouncementService crmPlatformAnnouncementService;
    @Autowired
    private CustomerService customerService;

    @Autowired
    AddressService addressService;

    @RequestMapping(value = "list")
    public String list(Model model) {
        return "modules/yangguang/systemSetting/notification/list";
    }

    @RequestMapping(value = "listData")
    @ResponseBody
    public Object listData(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        TrimStrMap parasMap = new TrimStrMap(getParasMap(request));
        crmPlatformAnnouncementService.listData(parasMap, page);
       /* List<Record> data = page.getData();
        for (Record r : data) {
            r.set("_obj_type", TranslationUtils.transAnnouncementObjType(r.getStr("obj_type")));
        }*/

        return page.renderJson();
    }

    @RequestMapping(value="userNotication")
    public String toUserNotication(){
        return "modules/yangguang/systemSetting/notification/userNoticationList";
    }


    /**
     * 通知公告
    */
    @RequestMapping(value = "noticeList")
    public String noticeList(Model model) {
        return "modules/yangguang/systemSetting/notification/noticeList";
    }


    @RequestMapping(value = "listTongzhi")
    public String listTongzhi(Model model) {
        return "modules/yangguang/msgcenter/listTongzhi";
    }

    @RequestMapping(value = "listTongzhiData")
    @ResponseBody
    public String listTongzhiData(HttpServletRequest request) {
        LayuiPage<Record> page = new LayuiPage<>(request);
        TrimStrMap parasMap = new TrimStrMap(getParasMap(request));
        parasMap.put("objType","3");
        crmPlatformAnnouncementService.listMessageData(parasMap, page);
        return page.renderJson();
    }


    @RequestMapping(value = "new")
    public String newAnnouncement(Model model,HttpServletRequest request) {
        String type = request.getParameter("type");//type==1 表示与用户的通知公告
        List<Record> pros = addressService.getProvinces();
        model.addAttribute("type",type);
        model.addAttribute("pros",pros);
        //获取所有用户
        List<Record> cus = customerService.getCustomerLists();
        model.addAttribute("cus",cus);
       /* if("3".equals(type)){
            return "modules/yangguang/systemSetting/notification/addthree";
        }*/
        return "modules/yangguang/systemSetting/notification/add";
    }

    @RequestMapping(value = "edit")
    public String edit(HttpServletRequest request, Model model) {
        model.addAttribute("id", request.getParameter("id"));
        CrmPlatformAnnouncement announcement = crmPlatformAnnouncementService.get(request.getParameter("id"));
       /* String objType = announcement.getObjType();
        String[] split = objType.split(",");*/
   /*     model.addAttribute("siteChecked", hasValue("1", split));
        model.addAttribute("factoryChecked", hasValue("2", split));*/
        model.addAttribute("announcement", announcement);
        return "modules/yangguang/systemSetting/notification/edit";
    }

    private boolean hasValue(String val, String[] vals) {
        for (String v : vals) {
            if (val.equals(v)) {
                return true;
            }
        }

        return false;
    }

    @RequestMapping(value = "show")
    @ResponseBody
    public Object show(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        model.addAttribute("id", id);
        return crmPlatformAnnouncementService.get(id);
    }

    @RequestMapping(value = "create")
    @ResponseBody
    public Result<Void> create(HttpServletRequest request, @Valid NotificationForm form, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail("422", "binding error");
        }
        //如果条件都为空则不给保存
        GuardUtils.notBlank(form.getHtml(),"html is null");
        if("1".equals(form.getObjType())){
            if(StringUtil.isEmpty(form.getProvince())&&StringUtil.isEmpty(form.getCustomerId())&&form.getLoginNumMin()==null&&form.getLoginNumMax()==null&&form.getStationBuildingStart()==null&&form.getStationBuildingEnd()==null){
                return Result.fail("402","推送群体为空");
            }
        }
        String html = request.getParameter("html");
        form.setHtml(html); // 必须的，不能去除
        return crmPlatformAnnouncementService.create(form);
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public Result<Void> update(HttpServletRequest request, @Valid NotificationForm form, BindingResult result) {
        if (result.hasErrors()) {
            return Result.fail("422", "binding error");
        }

        String html = request.getParameter("html");
        form.setHtml(html); // 必须的，不能去除
        return crmPlatformAnnouncementService.update(form);
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public Result<Void> delete(HttpServletRequest request) {
        String ids = request.getParameter("id");
        return crmPlatformAnnouncementService.delete(ids);
    }

    //技术资料菜单
    @RequestMapping("technicalData")
    public String technicalData(HttpServletRequest request,Model model) {
        return "modules/yangguang/systemSetting/technicalDataIndex";
    }

    /**
     * 文件管理
    */
    @RequestMapping("fileManagement")
    public String fileManagement(HttpServletRequest request,Model model) {
        return "modules/yangguang/systemSetting/fileManagement";
    }

    @ResponseBody
    @RequestMapping("ajaxTechnicalData")
    public String ajaxTechnicalData(HttpServletRequest request, Model model){
        Map<String, String> map = getParams(request);

        List<Record> rds = crmPlatformAnnouncementService.getTechnicalData(map);
        Long cnt = crmPlatformAnnouncementService.countTechnicalData(map);
        LayuiPage page = new LayuiPage(request);
        page.setData(rds);
        page.setCount(cnt);
        return page.renderJson();
    }
    @RequestMapping("technicalDataForm")
    public String technicalDataForm(HttpServletRequest request,Model model) {
        model.addAttribute("type","1");
        return "modules/yangguang/systemSetting/technicalDataForm";
    }

    @RequestMapping("fileManagementForm")
    public String fileManagementForm(HttpServletRequest request,Model model) {
        model.addAttribute("type","2");
        return "modules/yangguang/systemSetting/fileManagementForm";
    }

    @ResponseBody
    @RequestMapping("delTechnicalData")
    public String delTechnicalData(HttpServletRequest request, Model model){
        Map<String, String> map = getParams(request);
        User user = UserUtils.getUser();
        String ret = crmPlatformAnnouncementService.delTechnicalData(map);
        return ret;
    }

    @ResponseBody
    @RequestMapping("saveTechnicalData")
    public String saveTechnicalData(HttpServletRequest request, Model model){
        Map<String, String> params = getParams(request);
        String ret = crmPlatformAnnouncementService.saveTechnicalData(params);
        return ret;
    }

    /**
     * 获取最新通知
    */
    @ResponseBody
    @RequestMapping("findAnnouncementOne")
    public Record findAnnouncementOne(){
        User user = UserUtils.getUser();
        if(User.USER_TYPE_SUPPLI.equals(user.getUserType())){
            return null;
        }

        return crmPlatformAnnouncementService.findAnnouncementOne(user.getId());
    }


    //首页获取各种通知公告 限制5条
    @ResponseBody
    @RequestMapping(value = "indexGetAnnounceList")
    public Result indexGetAnnounceList(){
        Result result=new Result();
        User user = UserUtils.getUser();
        if(User.USER_TYPE_SUPPLI.equals(user.getUserType())){
            return null;
        }
        List<Record> list= crmPlatformAnnouncementService.indexGetAnnounceList(user.getId());
        result.data(list).setOk();
        return result;
    }

}
