package com.sifang.modules.smallprogram;


import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.entity.SiteReceiptInformation;
import com.sifang.modules.sunshine.SystemSetting.service.SiteReceiptInformationService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.site.siteInfo.service.SiteCompanyInfoService;
import com.sifang.modules.smallprogram.service.SmallProgramOrderService;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.entity.CrmPlatformAnnouncement;
import com.sifang.modules.sys.service.CrmPlatformAnnouncementService;
import com.sifang.modules.sys.util.CrmSiteUtils;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping(value="${adminPath}/main/mymodel")
public class SmallProgrammMyModelControl extends BaseController {
    @Autowired
    SiteReceiptInformationService erpCashOutService;
    @Autowired
    private SiteReceiptInformationService receService;
    @Autowired
    private SiteCompanyInfoService siteCompanyInfo;
    @Autowired
    private SmallProgramOrderService smallProgramOrderService;
    @Autowired
    CrmPlatformAnnouncementService crmPlatformAnnouncementService;

    /**
     * 获取服务商的可请款金总额  ***
     */
    @ResponseBody
    @RequestMapping(value="getSiteCash")
    public Object getSiteCash(HttpServletRequest request){
        Map<String,Object> map=new HashMap<>();
        String siteId=request.getParameter("siteId");
        map.put("cash",erpCashOutService.getSiteInformation(siteId));
        map.put("cashAccount",receService.getSiteReceiptInformation(siteId));
        return map;
    }
    /**
     * 服务商申请请款  ***
     */
    @ResponseBody
    @RequestMapping(value = "saveSiteCashOut")
    public Object saveSiteCashOut(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        String siteId=request.getParameter("siteId");
        Record site = CrmSiteUtils.getSiteBySiteId(map.get("siteId"));
        Result result=new Result();
        if(site ==null){
            result.setCode("401");
            return result;
        }
        map.put("applicantName",site.getStr("name"));
        map.put("applicantId",siteId);
         result = receService.applicantCashOut(map);
        return result;
    }
/**
 * 获取服务商的公司资料
 */
    @ResponseBody
    @RequestMapping(value="getCompanyInformation")
    public Object getCompanyInformation(HttpServletRequest request){
        String siteId=request.getParameter("siteId");
        Record site = CrmSiteUtils.getSiteBySiteId(siteId);
        return site;
    }
    /**
     * 修改资料保存
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "saveCompanyInfo")
    public Object saveCompanyInfo(HttpServletRequest request){
        return siteCompanyInfo.saveBtn(request);
    }


    /**
     *获取服务商的体现记录
     */
    @ResponseBody
    @RequestMapping(value = "getCrmSiteCashOutList")
    public Object getCrmSiteCashOutList(HttpServletRequest request){
        Map<String,String> map = getParams(request);
        String siteId = request.getParameter("siteId");
        LayuiPage page = new LayuiPage(request);
        List<Record> list =receService.getCashOutList(page,map,siteId);
        /*receService.getCashOutPage(page,map,siteId);*/
        return list;
    }
    /**
     * 获取服务商已请款(已到账) 和 待处理金额
     */
    @ResponseBody
    @RequestMapping(value = "getSiteAmount")
    public Record getSiteAmount(HttpServletRequest request){
        String siteId=request.getParameter("siteId");
        Record record=smallProgramOrderService.getSiteAmount(siteId);
        return record;
    }
    /**
     * 获取服务商的收款信息
     */
    @ResponseBody
    @RequestMapping(value="receiptInformation")
    public Object receiptInformation(HttpServletRequest request){
        String siteId=request.getParameter("siteId");
        SiteReceiptInformation rece = receService.getBySiteId(siteId);
        return  rece;
    }

    /**
     *保存修改的收款信息
     */
    @ResponseBody
    @RequestMapping(value = "saveSiteReceipt")
    public Object saveSiteReceipt(HttpServletRequest request, SiteReceiptInformation siterece){
        String siteId = request.getParameter("siteId");
        if(StringUtil.isBlank(siteId)){
            return Result.fail("500","site");
        }
        String code = "200";
        try {
            siterece.setSiteId(siteId);
            receService.saveSiteReceipt(siterece);
        }catch (Exception e){
            code = "205";
            e.printStackTrace();
            throw e;
        }
        return Result.fail(code,"site");
    }

    /**
     *获取通知公告
     */
    @RequestMapping(value = "listAnnounceData")
    @ResponseBody
    public Object listAnnounceData(HttpServletRequest request) {
        Map<String,String> map = getParams(request);
      String userId=request.getParameter("userId");
        String objType="2"; // 通知群体为服务商
        LayuiPage<Record> page = new LayuiPage<>(request);
        return smallProgramOrderService.listMessageData(userId,objType,map,page);
    }
    /**
     * 获取通知公告详情
     */
    @ResponseBody
    @RequestMapping(value="getAnnounceDetail")
    public Object getAnnounceDetail(HttpServletRequest request){
        if(StringUtil.isBlank(request.getParameter("id"))){
            return new CrmPlatformAnnouncement();
        }
        Map<String,Object> map = Maps.newHashMap();
        CrmPlatformAnnouncement announcement = crmPlatformAnnouncementService.get(request.getParameter("id"));
            map.put("annou",announcement);
            map.put("time", DateUtils.formatDateTime(announcement.getCreateTime()));
        return  map;
    }
    /**
     * 标记已读的通知公告
     */
    @RequestMapping(value = "markMsgAsRead")
    @ResponseBody
    public Result<Void> markMsgAsRead(HttpServletRequest request) {
        String id = request.getParameter("id");
        String siteId=request.getParameter("siteId");
        String userId=request.getParameter("userId");
        return smallProgramOrderService.markAnnounce(id,userId,"5",siteId);
    }
    /**
     获取技术资料
     */
    @ResponseBody
    @RequestMapping("ajaxTechnicalData")
    public List<Record> ajaxTechnicalData(HttpServletRequest request, Model model){
        Map<String, String> map = getParams(request);
        map.put("type","1");
        List<Record> rds = crmPlatformAnnouncementService.getTechnicalData(map);
        return rds;
    }

}
