package com.yunfeiyun.agmp.fms.service.impl;

import com.yunfeiyun.agmp.common.utils.DateUtils;
import com.yunfeiyun.agmp.common.utils.LunchCalenderUtil;
import com.yunfeiyun.agmp.fms.domain.FmsSolar;
import com.yunfeiyun.agmp.fms.mapper.FmsSolarMapper;
import com.yunfeiyun.agmp.fms.service.IFmsSolarService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 节气时令Service业务层处理
 * 
 * @author 杨晓辉
 * @date 2023-06-01
 */
@Service
public class FmsSolarServiceImpl implements IFmsSolarService
{
    private static final Logger log = LoggerFactory.getLogger(FmsSolarServiceImpl.class);

    @Autowired
    private FmsSolarMapper fmsSolarMapper;

    /**
     * 查询节气时令
     * 
     * @param solarId 节气时令主键
     * @return 节气时令
     */
    @Override
    public FmsSolar selectFmsSolarBySolarId(String solarId)
    {
        return fmsSolarMapper.selectFmsSolarBySolarId(solarId);
    }

    @Override
    public FmsSolar selectFmsSolarBySolarName(String solarName) {
        return fmsSolarMapper.selectFmsSolarBySolarName(solarName);
    }

    /**
     * 查询节气时令列表
     * 
     * @param fmsSolar 节气时令
     * @return 节气时令
     */
    @Override
    public List<FmsSolar> selectFmsSolarList(FmsSolar fmsSolar)
    {
        return fmsSolarMapper.selectFmsSolarList(fmsSolar);
    }

    /**
     * 新增节气时令
     * 
     * @param fmsSolar 节气时令
     * @return 结果
     */
    @Override
    public int insertFmsSolar(FmsSolar fmsSolar)
    {
        return fmsSolarMapper.insertFmsSolar(fmsSolar);
    }

    /**
     * 修改节气时令
     * 
     * @param fmsSolar 节气时令
     * @return 结果
     */
    @Override
    public int updateFmsSolar(FmsSolar fmsSolar)
    {
        return fmsSolarMapper.updateFmsSolar(fmsSolar);
    }

    /**
     * 批量删除节气时令
     * 
     * @param solarIds 需要删除的节气时令主键
     * @return 结果
     */
    @Override
    public int deleteFmsSolarBySolarIds(String[] solarIds)
    {
        return fmsSolarMapper.deleteFmsSolarBySolarIds(solarIds);
    }

    /**
     * 删除节气时令信息
     * 
     * @param solarId 节气时令主键
     * @return 结果
     */
    @Override
    public int deleteFmsSolarBySolarId(String solarId)
    {
        return fmsSolarMapper.deleteFmsSolarBySolarId(solarId);
    }

    @Override
    public int updateFmsSolarAll()  {
        try {
            List<FmsSolar> fmsSolarList = fmsSolarMapper.selectFmsSolarList(null);
            LunchCalenderUtil lunchCalenderUtil = new LunchCalenderUtil();
            Calendar rightNow = Calendar.getInstance();
            int year = rightNow.get(Calendar.YEAR);// 获取当前年份
            lunchCalenderUtil.setyear(year);
            int month = 1;// 获取当前月份
            LinkedHashMap<String, String> map = new LinkedHashMap<>();
            while (month <= 12) {
                lunchCalenderUtil.setmonth(month);
                String[] day = lunchCalenderUtil.getCalendar();// 阳历日期
                String[] chineseDay = lunchCalenderUtil.getchineseCalendar_festival_solarterms();// 农历日期
                ArrayList<String> listYl = new ArrayList<>();
                ArrayList<String> listNl = new ArrayList<>();
                boolean bool = false;
                int n = 0;
                for (String s : day) {
                    listYl.add(s);
                    n++;
                    if (n == 7) {
                        int k = lunchCalenderUtil.getweekDay();
                        for (int j = 0; j < k; j++) {
                            listNl.add("");
                        }
                        listNl.addAll(Arrays.asList(chineseDay).subList(k, 7));
                    }
                    if (n % 7 == 0 && n != 7) {
                        bool = true;
                    }
                    if (bool) {
                        listNl.addAll(Arrays.asList(chineseDay).subList((n - 7), n));
                        bool = false;
                    }
                    if (n == day.length && n % 7 != 0) {
                        listNl.addAll(Arrays.asList(chineseDay).subList((n - n % 7), n));
                    }
                }
                for (int k = 0; k < listYl.size(); k++) {
                    if (Arrays.asList(LunchCalenderUtil.SOLAR_TERM).contains(listNl.get(k))) {
                        map.put(listNl.get(k), year + "-" + month +"-"+ listYl.get(k));
                    }
                }
                month++;
            }
            for(FmsSolar fmsSolar : fmsSolarList){
                fmsSolar.setSolarStartdate(DateUtils.turnDateFormat(map.get(fmsSolar.getSolarName()),"yyyy-M-d","yyyy-MM-dd"));
            }
            return fmsSolarMapper.updateFmsSolarByBatch(fmsSolarList);
        }catch (Exception e){
            log.error("更新失败",e);
            return 0;
        }
    }

    @Override
    public FmsSolar getNowSolar(String date) {
        FmsSolar fmsSolar = fmsSolarMapper.getFmsSolarByDate(date);
        if(null == fmsSolar){
            return fmsSolarMapper.getFmsSolarByDate(null);
        }
        return fmsSolar;
    }

    @Override
    public Map<String, Object> getSysZoneByZoneCode(String zoneCode) {
        return fmsSolarMapper.getSysZoneByZoneCode(zoneCode);
    }


}
