package com.jeeplus.modules.API.works.pushInfo;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.persistence.Page;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.web.BaseController;
import com.jeeplus.modules.appversion.service.AppversionsService;
import com.jeeplus.modules.pushinfo.entity.Pushinfo;
import com.jeeplus.modules.pushinfo.service.PushinfoService;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.sysuseroffice.entity.Useroffice;
import com.jeeplus.modules.utils.ErrorCode;
import com.jeeplus.modules.workcompanyinfo.service.CompanyinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${frontPath}/pushInfo")
public class pushInfoCortroller extends BaseController {

	@Autowired
	private PushinfoService pushinfoService;
    @Autowired
    private AppversionsService appversionsService;
    @Autowired
    private CompanyinfoService companyinfoService;

	@ResponseBody
	@RequestMapping(value = "pushInfoList", method = RequestMethod.GET)
	public AjaxJson pushInfoList(HttpServletRequest request, HttpServletResponse response) {
		AjaxJson json = new AjaxJson();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HashMap requestMap = findRequestMap(request);
        String requestType = (String)requestMap.get("type");
        String mobile  = (String)requestMap.get("mobile");
        String companyId  = (String)requestMap.get("companyId");
        String pageNo  = (String)requestMap.get("pageNo");
        try {
            Pushinfo pushinfo = new Pushinfo();
            User user = UserUtils.getUser();
            pushinfo.setDelFlag("0");
            if (requestType!=null && requestType.equals("1")){
                pushinfo.setParentType("all");
                pushinfo.setPushUserId("allpushusers");
            }else {
                pushinfo.setPushUserId(user.getId());
                pushinfo.setParentType("single");
            }
            if (mobile!=null){
                if (mobile.equals("ios")){
                    pushinfo.setMobile("ios");
                }else if (mobile.equals("android")){
                    pushinfo.setMobile("android");
                }
            }
            if (companyId!=null){
                pushinfo.setCompanyId(companyId);
            }
            Page<Pushinfo> page = pushinfoService.findPushList(new Page<Pushinfo>(request, response), pushinfo);
            List<Pushinfo> list = page.getList();
            List<Map> mapList = new ArrayList<>();
            if(list==null){
                json.setSuccess(false);
                json.setErrorCode(ErrorCode.code_2005);
                json.setMsg("获取列表失败");
                json.put("data", mapList);
            }else {
                json.put("maxNum", page.getCount());
                if(Integer.parseInt(pageNo)<=page.getTotalPage()) {
                    for (Pushinfo p : list) {
                        Map map = new HashMap();
                        String type = p.getType();
                        String createDate = sdf.format(p.getCreateDate()).substring(5, 10);
                        String url = p.getAddcontent() == null ? "" : p.getAddcontent();
                        map.put("type", p.getType() == null ? "" : p.getType());
                        map.put("title", p.getTitle() == null ? "" : p.getTitle());
                        map.put("content", p.getContent() == null ? "" : p.getContent());
                        map.put("contents", p.getRemarks() == null ? "" : p.getRemarks());
                        map.put("remarks", p.getStatus() == null ? "" : p.getStatus());
                        map.put("id", p.getPushId() == null ? "" : p.getPushId());
                        map.put("createDate", createDate == null ? "" : createDate);
                        map.put("parentType", p.getParentType() == null ? "" : p.getParentType());
                        String state = "";
                        String procDefKey = "";
                        String img = "";
                        if (type.equals("2001") && p.getRemarks() != null) {
                            url = "";
                            if (StringUtils.isNotBlank(p.getAddcontent()) && (p.getAddcontent().contains("&gt;") || p.getAddcontent().contains("<p>"))) {
                                state = "1";
                            } else {
                                state = "2";
                            }
                            //img = p.getAddcontent() == null || p.getAddcontent().equals("") ? "" : p.getAddcontent();
                        } else if (type.contains("400") && p.getAddcontent() != null) {
                            procDefKey = p.getAddcontent();
                        } else if (type.equals("2002")){  //加入企业申请
                            procDefKey = p.getAddcontent();
                            Pushinfo pushinfo1 = new Pushinfo();
                            pushinfo1.setPushId(p.getPushId());
                            List<Pushinfo> pushinfos = pushinfoService.getByPushId(pushinfo1);
                            try {
                                Useroffice companyinfo  = companyinfoService.get(p.getPushId());
                                if (pushinfos!=null && pushinfos.size()!=0) {
                                    Pushinfo push = pushinfos.get(0);
                                    if (StringUtils.isNotBlank(push.getStatus())&& push.getStatus().equals("1")){
                                        map.put("selectStatus", "1");
                                        map.put("officeName", companyinfo.getCreateBy().getOffice().getName());
                                        map.put("comment", companyinfo.getRemarks());
                                        map.put("actStuatus", companyinfo.getStatus());
                                    }else {
                                        map.put("selectStatus", "0");
                                    }
                                }else {
                                    logger.error("获取推送列表失败");
                                    json.setSuccess(false);
                                    json.setErrorCode(ErrorCode.code_2004);
                                    json.setMsg("获取推送列表失败,当前申请不存在");
                                }
                            }catch (Exception e){
                                map = new HashMap();
                            }
                        }
                        map.put("url",url );
                        if (map!=null) {
                            map.put("state", state);
                            map.put("img", img);
                            map.put("procDefKey", procDefKey);
                            mapList.add(map);
                        }
                    }
                    json.put("data", mapList);
                }else {
                    json.put("data", new ArrayList<>());
                }
                json.setErrorCode(ErrorCode.code_1004);
            }
        }catch (Exception e){
            json.setSuccess(false);
            json.setErrorCode(ErrorCode.code_2004);
            json.setMsg("获取推送列表失败");
        }
		return json;
	}

    @ResponseBody
    @RequestMapping(value = "pushCompanyInfoList", method = RequestMethod.GET)
    public AjaxJson pushCompanyInfoList(String mobile,HttpServletRequest request, HttpServletResponse response) {
        AjaxJson json = new AjaxJson();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (mobile==null || mobile.equals("")){
                json.setSuccess(false);
                json.setErrorCode(ErrorCode.code_2003);
                json.setMsg("获取失败：参数mobile不能为空");
                return json;
            }
            User user = UserUtils.getUser();
            List<Office> officeList = UserUtils.getAllCompany(user);
            List<Map> mapList = new ArrayList<>();
            if (officeList!=null && officeList.size()!=0){
                for (Office office :officeList) {
                    Pushinfo pushinfo = new Pushinfo();
                    String companyId = office.getId();
                    if (!companyId.equals("1")) {
                        pushinfo.setCompanyId(companyId);
                        pushinfo.setDelFlag("0");
                        pushinfo.setPushUserId(user.getId());
                        pushinfo.setParentType("single");
                        pushinfo.setMobile(mobile);
                        List<Pushinfo> list = pushinfoService.findList(pushinfo);
                        if (list != null && list.size() != 0) {
                            Pushinfo p = list.get(0);
                            Map map = new HashMap();
                            String type = p.getType();
                            String createDate = sdf.format(p.getCreateDate()).substring(5, 10);
                            map.put("type", p.getType() == null ? "" : p.getType());
                            map.put("title", p.getTitle() == null ? "" : p.getTitle());
                            map.put("content", p.getContent() == null ? "" : p.getContent());
                            map.put("contents", p.getRemarks() == null ? "" : p.getRemarks());
                            map.put("remarks", p.getStatus() == null ? "" : p.getStatus());
                            map.put("id", p.getPushId() == null ? "" : p.getPushId());
                            map.put("createDate", createDate == null ? "" : createDate);
                            map.put("parentType", p.getParentType() == null ? "" : p.getParentType());
                            map.put("companyId", p.getCompanyId());
                            String state = "";
                            String procDefKey = "";
                            String img = "";
                            if (type.equals("2001") && p.getRemarks() != null) {
                                if (p.getRemarks().contains("&gt;")) {
                                    state = "1";
                                } else {
                                    state = "2";
                                }
                                img = p.getAddcontent() == null || p.getAddcontent().equals("") ? "" : p.getAddcontent();
                            } else if (type.contains("400") && p.getAddcontent() != null) {
                                procDefKey = p.getAddcontent();
                            }
                            map.put("state", state);
                            map.put("img", img);
                            map.put("procDefKey", procDefKey);
                            map.put("officeName", office.getName());
                            map.put("officeLoge", office.getLogo() == null || office.getLogo().equals("") ? "" : office.getLogo());
                            mapList.add(map);
                        }
                    }
                }
            }
            json.put("data", mapList);
            json.setErrorCode(ErrorCode.code_1004);
        }catch (Exception e){
            json.setSuccess(false);
            json.setErrorCode(ErrorCode.code_2004);
            json.setMsg("获取公司推送列表失败");
        }
        return json;
    }

}
