package com.sifang.modules.account.controller;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.account.service.TotalAssetDetailsService;
import com.sifang.modules.setting.service.MemberService;
import com.sifang.modules.sunshine.SystemSetting.service.PlanService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartner;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.entity.Operator;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseController;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


//总资产明细

@Controller
@RequestMapping(value = "${adminPath}/accountMagement/accountDetail")
public class TotalAssetDetailsController extends BaseController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private TraderService traderService;

    @Autowired
    private PartnerService partnerService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private PlanService planService;

    @Autowired
    private TotalAssetDetailsService detailsService;

    @RequestMapping(value = "detailListPage")
    public String detailListPage(Model model){
        User user = UserUtils.getUser();
        //代理商列表
        List<Record> traderList = traderService.listTraderRecords(null, new HashMap<String, String>());
        model.addAttribute("traderList", traderList);

        //产品编号
        List<Record> projectList = null;
        CrmPartner partner;
        if(User.USER_TYPE_HZS.equals(user.getUserType())){
            String parId = CrmUtils.getPartnerId(user.getId());
            partner = partnerService.getById(parId);
            projectList = memberService.getProjectListByIds(partner.getAuthorizedId());
        }else if(User.USER_TYPE_PARTNER.equals(user.getUserType())){
            partner= partnerService.findByUserId(user.getId());
            projectList = memberService.getProjectListByIds(partner.getAuthorizedId());
        }else{
            projectList = memberService.getProjectLists();
        }
        model.addAttribute("projectList", projectList);

        //匹配方案
        List<Record> plans = planService.findAllPlan();

        model.addAttribute("plans", plans);
        return "modules/yangguang/account/accountManagement/detailListPage";
    }

    @ResponseBody
    @RequestMapping("/ajaxForWaitOrder")
    public Object ajaxForWaitOrder(HttpServletRequest request){
        Date now = new Date();
        String sn = DateUtils.formatDate(now, "yyyyMMddHHmmss");
        long s1 = System.currentTimeMillis();
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        User user = UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), user.getUserName(),user.getUserType());
        detailsService.entryListAccount(page, map, operator);
        long s2 = System.currentTimeMillis();
        logger.info(" >>获取项目信息列表 时间统计  [{}] end doSave use {}ms ", sn, (s2 - s1));
        return page.renderJson();
    }

    @RequestMapping(value = "toImportCustomerEntryGe")
    public String toImport() {
        return "modules/yangguang/account/accountManagement/importCustomerEntryGe";
    }

    @ResponseBody
    @RequestMapping(value = "checkImportCustomerEntryGe")
    public Object checkImportBarcode(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("extName", StringUtil.getFileExtName(file.getOriginalFilename()));
        Map<String, Object> retMap = detailsService.checkImportBarcode(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }

    @RequestMapping(value = "importCustomerEntryGe")
    @ResponseBody
    public Object importBarcode(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("fileName", file.getOriginalFilename());
        Map<String, Object> retMap = detailsService.importBarcode(params, file.getInputStream());
        return retMap;
    }

}
