package com.sifang.modules.smallprogram.entry.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CustomerEntryService;

import com.sifang.modules.sunshine.SystemSetting.entity.CrmTraderEmploye;

import com.sifang.modules.sunshine.SystemSetting.service.TraderEmployeService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.partnerManagement.entity.CrmPartner;
import com.sifang.modules.sunshine.partnerManagement.service.PartnerService;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.dao.UserDao;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 电站
 */
@RestController
@RequestMapping(value = "${adminPath}/miniProgram")
public class MpCustomerEntryController extends BaseController {

    @Autowired
    private UserDao userDao;

    @Autowired
    private PartnerService partnerService;

    @Autowired
    private TraderEmployeService traderEmployeService;

    @Autowired
    private CustomerEntryService customerEntryService;

    /**
     * 电站安装列表
     */
    @RequestMapping(value = "waitInstallEntry")
    public Result<List<Map<String, Object>>> waitInstallEntry(HttpServletRequest request) {
        Map<String, String> params = getParams(request);
        String userId = params.get("userId");
        User user = userDao.getById(userId);
        if(User.USER_TYPE_HZS.equals(user.getUserType())){
            String parId = CrmUtils.getPartnerId(user.getId());
            CrmPartner partner = partnerService.getById(parId);
            params.put("authorizedId", partner.getAuthorizedId());
        }else if(User.USER_TYPE_PARTNER.equals(user.getUserType())){
            CrmPartner partner = partnerService.findByUserId(user.getId());
            params.put("authorizedId", partner.getAuthorizedId());
        } else if (User.USER_TYPE_ADVISOR.equals(user.getUserType())) {
            CrmTraderEmploye employe = traderEmployeService.findByUserId(user.getId());
            params.put("engineerId", employe.getId());
        }
        Operator operator = Operator.of(userId, CrmUtils.getUserXM(user), user.getUserType());
        LayuiPage<Record> page = new LayuiPage<Record>(request);
        customerEntryService.listWaitOrder(page, params, operator);
        List<Map<String, Object>> retList = CrmUtils.turnRecordListToMapList(page.getData());
        return Result.success(retList);
    }

    /**
     * 提交安装验收信息
     */
    @RequestMapping(value = "submitAcceptanceInfo")
    public Result submitAcceptanceInfo() {

        return Result.ok();
    }

}
