package cn.com.countrygarden.bi.main.service.marketing.impl;

import cn.com.countrygarden.bi.main.core.AbstractService;
import cn.com.countrygarden.bi.main.dao.marketing.CommReportWeeklyMapper;
import cn.com.countrygarden.bi.main.model.PageData;
import cn.com.countrygarden.bi.main.model.common.CommConstant;
import cn.com.countrygarden.bi.main.model.marketing.CommReportWeekly;
import cn.com.countrygarden.bi.main.model.marketing.CommReportWeeklySummary;
import cn.com.countrygarden.bi.main.service.common.CommService;
import cn.com.countrygarden.bi.main.service.marketing.CommReportWeeklyService;
import cn.com.countrygarden.bi.main.util.DateUtil;
import com.github.pagehelper.PageRowBounds;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
@Transactional
public class CommReportWeeklyServiceImpl extends AbstractService<CommReportWeekly> implements CommReportWeeklyService {

    @Autowired
    private CommReportWeeklyMapper mapper;

    @Autowired
    private CommService commService;

    public Map<String, Object> insertOrUpdate(int commId, int woy, int year, String workContent, String workPlan, String currentAssistanceIssues, String userCode) {
        int weekOfYear = woy > 0 ? woy : DateUtil.getWeekOfYear();
        int thisYear = year > 0 ? year : DateUtil.getYear();
        Map<String, Object> data = new HashMap<String, Object>() {{
            put("commId", commId);
            put("weekOfYear", weekOfYear);
            put("year", thisYear);
            put("workContent", workContent);
            put("workPlan", workPlan);
            put("currentAssistanceIssues", currentAssistanceIssues);
            put("userCode", userCode);
            put("date", new Date());
            put("startDate", DateUtil.simpleFormat(
                    DateUtil.getFirstDayOfWeek(thisYear, weekOfYear)
            ));
            put("endDate", DateUtil.simpleFormat(
                    DateUtil.getLastDayOfWeek(thisYear, weekOfYear)
            ));
        }};

        mapper.insertOrUpdate(data);

        return data;
    }

    /**
     * 周报列表
     *
     * @param commId
     * @param limit
     * @return
     */
    public List<CommReportWeekly> search(int commId, int year, int limit) {
        int thisYear = year > 0 ? year : DateUtil.getYear();
        return mapper.search(new HashMap<String, Object>() {{
            put("commId", commId);
            put("year", thisYear);
            put("limit", limit);
        }});
    }

    public List<Map<String, Object>> getLastWeeklyReport(int commId, int year, int limit) {
        List<CommReportWeekly> reportWeeklies = search(commId, year, limit);
        List<Map<String, Object>> reports = new ArrayList<>();
        for (CommReportWeekly reportWeekly : reportWeeklies) {
            reports.add(getReportItem(reportWeekly));
        }
        return reports;
    }

    public CommReportWeekly getCurrentReport(int commId) {
        return mapper.getCurrentReport(new HashMap<String, Object>() {{
            put("commId", commId);
            put("weekOfYear", DateUtil.getWeekOfYear());
            put("year", DateUtil.getYear());
        }});
    }

    public Map<String, Object> getCurrentReportMap(int commId) {
        CommReportWeekly reportWeekly = getCurrentReport(commId);
        if (reportWeekly == null) {
            return new HashMap<>();
        }

        return getReportItem(reportWeekly);
    }

    private Map<String, Object> getReportItem(CommReportWeekly reportWeekly) {
        Date startDate = reportWeekly.getStartDate();
        Date endDate = reportWeekly.getStartDate();
        return new HashMap<String, Object>() {{
            put("id", reportWeekly.getId());
            put("weekOfYear", reportWeekly.getWoy());
            put("year", reportWeekly.getYear());
            put("workContent", reportWeekly.getContent());
            put("workPlan", reportWeekly.getNextWeekPlan());
            put("currentAssistanceIssues", reportWeekly.getAssistanceItem());
            put("startDate", startDate != null ? DateUtil.simpleFormat(startDate) : "");
            put("endDate", endDate != null ? DateUtil.simpleFormat(endDate) : "");
        }};
    }

    @Override
    public PageData searchSummary(String userCode, String searchKey,
                                  String organId,
                                  String contractorName,
                                  String startDteStr,
                                  String endDateStr,
                                  String state,
                                  int skipe,
                                  int limit) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Map<String, Object> organMap = commService.findOrganMapCache();
        Map<String, Object> query = new HashMap<String, Object>();
        if (!StringUtils.isEmpty(searchKey)) {
            query.put("searchKey", searchKey);
        }
        if (!StringUtils.isEmpty(organId)) {
            query.put("organId", organId);
        }
        if (!StringUtils.isEmpty(contractorName)) {
            query.put("contractorName", contractorName);
        }
        if (!StringUtils.isEmpty(startDteStr) & !StringUtils.isEmpty(endDateStr)) {
            Date startDate = DateUtil.parseDate(startDteStr);
            Date endDate = DateUtil.parseDate(endDateStr);
            query.put("startDate", startDate);
            query.put("endDate", endDate);
        }
        if (!StringUtils.isEmpty(state)) {
            query.put("state", state);
        }

        PageRowBounds pageRowBounds = new PageRowBounds(skipe, limit);
        List<CommReportWeeklySummary> dataList = mapper.fincSummaryByWeek(query, pageRowBounds);
        for (CommReportWeeklySummary data : dataList) {
            //区域、状态、面积
            String organName = (String) organMap.get(data.getOrganId());
            data.setOrganName(organName);
            if (data.getPlanningArea() != null) {
                BigDecimal area = data.getPlanningArea();

//                area = area.divide(new BigDecimal(10000), 2);
                data.setPlanningArea(area);
            }
            String status = CommConstant.CommStatus.statusName.get(data.getState());
            data.setState(status);

            String dateRange = String.format("%s至%s", startDteStr, endDateStr);
            data.setDateRange(dateRange);
            if (data.getHistoryCount() != null && data.getHistoryCount() > 0) {
                data.setStateChange(true);
            }
            if(data.getWeekWorkContent()==null){
                data.setWeekWorkContent("-");
            }
            if(data.getNextWeekPlan()==null){
                data.setNextWeekPlan("-");
            }
            if(data.getAssistantContent()==null){
                data.setAssistantContent("-");
            }
        }
        return new PageData(dataList, pageRowBounds);
    }
}
