package com.sifang.modules.customer.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CrmCustomerEntryRentManagementService;
import com.sifang.modules.sunshine.SystemSetting.service.TraderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
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 com.sifang.modules.task.DailyRecordTask;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "${adminPath}/customerEntryRentManagement")
public class CrmCustomerEntryRentManagementController  extends BaseController {
    @Autowired
    TraderService traderService;
    @Autowired
    CrmCustomerEntryRentManagementService rentManagementService;
    @RequestMapping(value = "findRentList")
    public String findRentList(Model model){
        //代理商列表
        List<Record> traderList = traderService.listTraderRecords(null, new HashMap<String, String>());
        model.addAttribute("traderList", traderList);
        return "modules/yangguang/customer/entry/customerEntryRentList";
    }


    @ResponseBody
    @RequestMapping(value = "getRentListData")
    public Object getRentListData(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> params = getParams(request);
        User user = UserUtils.getUser();
        Operator operator = Operator.of(user.getId(), CrmUtils.getUserXM(user),user.getUserType());
        return rentManagementService.getPageData(page, params, operator);
    }

    /**
     * 同步租金页面
     */
    @RequestMapping("/syncRentListPage")
    public String syncRentListPage(){
        return "modules/yangguang/customer/entry/syncRentListPage";
    }
    /**
     * 同步租金
     */
    @ResponseBody
    @RequestMapping("/syncRentList")
    public Result syncRentList(String synchronizationType ,String dueDateFrom ,String dueDateTo ,String contractNumber){
        try{
            if (StringUtil.isNotBlank(dueDateFrom)){
                dueDateFrom = dueDateFrom.replace("-", "");
            }
            if (StringUtil.isNotBlank(dueDateTo)){
                dueDateTo = dueDateTo.replace("-","");
            }
            rentManagementService.dealRentList(synchronizationType,dueDateFrom,dueDateTo, contractNumber);
        }catch (IllegalArgumentException e){
            return Result.fail(e.getMessage());
        }
        return Result.ok();
    }
}
