package com.sifang.modules.platform.controller;



import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryService;
import com.sifang.modules.flowpath.service.IndexStaticApplyService;
import com.sifang.modules.platform.service.IndexService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.setting.service.CrmSupplyDeliveryMethodService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.web.BaseController;
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.ResponseBody;

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


@Controller
@RequestMapping(value = "${adminPath}/sys/toHomerPager")
public class IndexController extends BaseController {
    @Autowired
    IndexService indexService;

    @Autowired
    private IndexStaticApplyService indexStaticApplyService;

    @Autowired
    private CustomerEntryService entryService;

    @Autowired
    private CrmSupplyDeliveryMethodService methodService;

    @ResponseBody
    @RequestMapping(value="homerPager")
    public String toHomerPager(HttpServletRequest request){
        Map<String, String> params = getParams(request);
        User user = UserUtils.getUser();
        Ascription ascription = CrmUtils.getAscription(user);
        if (User.USER_TYPE_KJ.equals(user.getUserType())||User.USER_TYPE_ADVISOR.equals(user.getUserType())) {
           /* params.put("productLine", "1");*/
            params.put("ascriptionId", ascription.getAscriptionId());
        }
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user),user.getUserType());
        Record  rd=entryService.findSysHomeList(params,operator);
        return rd.toJson();
    }

    @ResponseBody
    @RequestMapping(value="homerPagerPancake")
    public String homerPagerPancake(HttpServletRequest request){
        Map<String, String> params = getParams(request);
        User user = UserUtils.getUser();
        Ascription ascription = CrmUtils.getAscription(user);
        if (User.USER_TYPE_KJ.equals(user.getUserType())||User.USER_TYPE_ADVISOR.equals(user.getUserType())) {
            params.put("ascriptionId", ascription.getAscriptionId());
        }
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user),user.getUserType());
        Record  rd=entryService.findSysPancakeList(params,operator);
        return rd.toJson();
    }


    @ResponseBody
    @RequestMapping(value="factory/homerPager")
    public String toFactoryHomerPager(){
       return "";
    }

    //首页待办任务统计
    @ResponseBody
    @RequestMapping(value = "staticApplyCount")
    public Result staticApplyCount(){
        Result result=Result.ok();
        User user = UserUtils.getUser();
        Map<String,Integer> resultMap= indexStaticApplyService.staticApplyCount(user);
        return result.data(resultMap);
    }
    /**
     * 物料统计
    */
    @ResponseBody
    @RequestMapping(value = "findMaterial")
    public Object findMaterial(HttpServletRequest request){
        Map<String, String> params = getParams(request);
        Result result=Result.ok();
        User user = UserUtils.getUser();
        Ascription ascription = CrmUtils.getAscription(user);
        if (User.USER_TYPE_KJ.equals(user.getUserType())||User.USER_TYPE_ADVISOR.equals(user.getUserType())) {
            params.put("traderId", ascription.getAscriptionId());
        }
        Record re = methodService.findMaterislAll(params);
        return result.data(re);
    }


}
