package com.sifang.modules.customer.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.customer.Service.CrmCustomerEntryDailyRecordService;
import com.sifang.modules.customer.entity.CrmCustomerEntryDailyRecord;
import com.sifang.modules.customer.excel.DailyRecodeExcel;
import com.sifang.modules.customer.excel.FileExportUtil;
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.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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "${adminPath}/customerEntryDailyRecord")
public class CrmCustomerEntryDailyRecordController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(CrmCustomerEntryDailyRecord.class);
    @Autowired
    private CrmCustomerEntryDailyRecordService dailyRecordService;

    @RequestMapping(value = "dailyRecordList")
    public String dailyRecordList(Model model){
        Date date = dailyRecordService.getMinDate();
        String minDate = "";
        if (date != null){
            minDate = DateUtils.formatDate(date, "yyyy-MM-dd");
        }
        String maxDate = DateUtils.dateChangeDays(new Date(), -1,"yyyy-MM-dd");
        model.addAttribute("maxDate",maxDate);
        model.addAttribute("minDate",minDate);
        return "modules/yangguang/customer/entry/provincialStatistics/dailyRecordList";
    }

    /**
     *   列表数据  getDailyRecordListData
     */
    @ResponseBody
    @RequestMapping("/getDailyRecordListData")
    public Object getDailyRecordListData(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 dailyRecordService.getPageData(page,params,operator);
    }

    /**
     * 去同步日报页面
     */
    @RequestMapping(value = "toSyncDailyRecord")
    public String toSyncDailyRecord(Model model){
        String now = DateUtils.dateChangeDays(new Date(), -1,"yyyy-MM-dd");
        model.addAttribute("now",now);
        return "modules/yangguang/customer/entry/provincialStatistics/syncDailyRecord";
    }
    /**
     * 同步日报
     */
    @ResponseBody
    @RequestMapping("/syncDailyRecord")
    public Result syncDailyRecord(HttpServletRequest request ){
        String creatTime = request.getParameter("creatTime");
        Date date = DateUtils.parseDate(creatTime);
        DailyRecordTask dailyRecordTask = new DailyRecordTask();
        dailyRecordTask.syncDailyRecord(date);
        return Result.ok();
    }


    /**
     * 导出excel
     */
    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletRequest request, HttpServletResponse response){
        String creatTime = request.getParameter("creatTime");
        System.out.println("creatTime----->"+creatTime);
        List<Record> dailyRecords = dailyRecordService.getDailyRecordsByDate(creatTime);
        List<DailyRecodeExcel> list = new ArrayList<>();
        for (Record record : dailyRecords) {
            DailyRecodeExcel excel = new DailyRecodeExcel();
            excel.setProvince(record.getStr("province"));
            excel.setLeaderName(record.getStr("leader_name"));
            excel.setSignedCntDay(record.getBigDecimal("signed_cnt_day"));
            excel.setCumSignedCnt(record.getBigDecimal("cum_signed_cnt"));
            excel.setSignedCntWithApp(record.getBigDecimal("signed_cnt_with_app"));
            excel.setShippedCntDay(record.getBigDecimal("shipped_cnt_day"));
            excel.setShippedCntMonth(record.getBigDecimal("shipped_cnt_month"));
            excel.setAppCntDay(record.getBigDecimal("app_cnt_day"));
            excel.setAppCntMonth(record.getBigDecimal("app_cnt_month"));
            excel.setSubmittedDay(record.getBigDecimal("submitted_day"));
            excel.setSubmittedMonth(record.getBigDecimal("submitted_month"));
            excel.setFinalApprovedDay(record.getBigDecimal("final_approved_day"));
            excel.setFinalApprovedMonth(record.getBigDecimal("final_approved_month"));
            excel.setGridConnectedDay(record.getBigDecimal("grid_connected_day"));
            excel.setGridConnectedMonth(record.getBigDecimal("grid_connected_month"));
            list.add(excel);
        }
        FileExportUtil.exportDailyRecodeExcel(list,response,creatTime);
    }


}
