package com.tofly.xrtygis.custom.service.impl;
import com.tofly.xrtygis.custom.entity.enums.EnumUserSfYsxzInfo;
import com.tofly.xrtygis.custom.entity.vo.CollectionRateVo;
import com.tofly.xrtygis.custom.entity.vo.LargeUseWaterVo;
import com.tofly.xrtygis.custom.mapper.LscreenMapper;
import com.tofly.xrtygis.custom.service.LscreenService;
import com.tofly.xrtygis.custom.service.YxdbCustomerRecordService;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Service
@AllArgsConstructor
public class LscreenServiceImpl implements LscreenService {
    @Autowired
    private LscreenMapper lscreenMapper;
    @Autowired
    private YxdbCustomerRecordService yxdbCustomerRecordService;
    @Override
    public  Map<String,Object> getUseWater() {
       String year=LocalDate.now().getYear()+"";
       String lastYear=LocalDate.now().getYear()-1+"";
        return lscreenMapper.getUseWater(year,lastYear);
    }

    @Override
    public List<Map<String, Object>> getUserCount() {
           List< Map<String, Object>> list=new ArrayList<>();
           Date now=new Date();
           String formatter="yyyyMM";
         for (Integer i=1;i<=12;i++){
             String yearAndMonth=getYearAndMonth(now,-i,formatter);
             Map<String, Object> vo=lscreenMapper.getUserCount(yearAndMonth);
             vo.put("cwrq",yearAndMonth);

             list.add(vo);
         }

        return list;
    }

    @Override
    public List<LargeUseWaterVo> getLargeUseWater(Integer topNum) {
        return lscreenMapper.getLargeUseWater(topNum);
    }

    @Override
    public List<Map<String, Object>> getUserYsXzAndMoney() {
        List<Map<String, Object>> list=new ArrayList<>();
        for (EnumUserSfYsxzInfo info:EnumUserSfYsxzInfo.values()
             ) {
            String name=info.getName();
            String typeName=info.getTypeName();
            List<String> typeNames=new ArrayList<>();
            String[] split = typeName.split(",");
            typeNames.addAll(Arrays.asList(split));

            Map<String, Object> map=lscreenMapper.getUserYsXzAndMoney(typeNames);
            map.put("mc",name);
            list.add(map);
        }

        return list;
    }


    @Override
    public Map<String, Object> getLastSfQf() {
        return lscreenMapper.getLastSfQf();
    }



    @Override
    public List<Map<String, Object>> GetMeterReadingRate(Integer num) {
        List<Map<String ,Object>> list=new ArrayList<>();
        Date now=new Date();
        String formatter="yyyyMM";
        for (Integer i=1;i<=num;i++){
            String yearAndMonth=getYearAndMonth(now,-i,formatter);
           Map<String ,Object>  map=lscreenMapper.GetMeterReadingRate(yearAndMonth);
            map.put("cwrq",yearAndMonth);
            list.add(map);
        }

       return list;
    }


    @Override
    public List<Map<String, Object>> getQf3Data() {
        //欠费超过3月数据统计
        List<Map<String ,Object>> list=new ArrayList<>();
        Date now=new Date();
        String formatter="yyyyMM";
        for (Integer i=0;i<=11;i++){
            String yearAndMonth=getYearAndMonth(now,-i,formatter);
            String yearAnd3Month=getYearAndMonth(now,-(i+3),formatter);
            Map<String ,Object>  map=lscreenMapper.getQf3Data(yearAnd3Month);
            map.put("cwrq",yearAndMonth);
            list.add(map);
        }
        return list;
    }


    @Override
    public List<Map<String, Object>> getNearNumber() {
        Integer startYear=LocalDate.now().getYear()-1;
        Integer endYear=LocalDate.now().getYear();
        return lscreenMapper.getNearNumber(startYear,endYear);

    }

    @Override
    public List<CollectionRateVo> GetCollectionRate() {

        Integer startYear=LocalDate.now().getYear()-1;
        Integer endYear=LocalDate.now().getYear();
        List<CollectionRateVo> list=lscreenMapper.GetCollectionRate(startYear,endYear);
        if(list!=null && list.size()>0){
            list.forEach(a->{
                if(a.getZje()==0F){
                    a.setCollectionrate(0F);
                }else{
                    float   cc  =  a.getSfje()/a.getZje();
                    float  collectionRate   =  (float)(Math.round(cc*10000))/10000;
                    a.setCollectionrate(collectionRate);
                }
            });
        }

        return list;
    }


    @Override
    public List<Map<String, Object>> getNearPay() {
        //去年，今年收费金额统计
        Integer startYear=LocalDate.now().getYear()-1;
        Integer endYear=LocalDate.now().getYear();
        return lscreenMapper.getNearPay(startYear,endYear);
    }


    @Override
    public List<Map<String, Object>> getqdPay(Integer month) {
        return lscreenMapper.getqdPay(month);
    }

    @Override
    public boolean saveCustomerData(String recordMonth) {
        List<String> list=Arrays.asList(recordMonth.split(","));
        list.forEach(item->yxdbCustomerRecordService.saveCustomerData(item));
        return true;
    }

    @Override
    public List<Map<String, Object>> getUserWaterType() {
        List<Map<String, Object>> list=new ArrayList<>();
        for (EnumUserSfYsxzInfo info:EnumUserSfYsxzInfo.values()
        ) {
            String name=info.getName();
            String typeName=info.getTypeName();
            List<String> typeNames=new ArrayList<>();
            String[] split = typeName.split(",");
            typeNames.addAll(Arrays.asList(split));

            Map<String, Object> map=lscreenMapper.getUserWaterType(typeNames);
            map.put("mc",name);
            list.add(map);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getSalesWaterTotal() {
        List<Map<String, Object>> list=new ArrayList<>();
        for (EnumUserSfYsxzInfo info:EnumUserSfYsxzInfo.values()
        ) {
            String name=info.getName();
            String typeName=info.getTypeName();
            List<String> typeNames=new ArrayList<>();
            String[] split = typeName.split(",");
            typeNames.addAll(Arrays.asList(split));

            Map<String, Object> map=lscreenMapper.getSalesWaterTotal(typeNames);
            map.put("mc",name);
            list.add(map);
        }

        return list;
    }

    @Override
    public Map<String, Object> getPayInfo() {
       return lscreenMapper.getPayInfo();
    }

    @Override
    public List<Map<String, Object>> getPayMoneyInfo() {
        //去年，今年收费金额统计
        Integer startYear=LocalDate.now().getYear()-1;
        Integer endYear=LocalDate.now().getYear();
         return lscreenMapper.getPayMoneyInfo(startYear,endYear);
    }

    @Override
    public List<Map<String, Object>> getPayUserInfo() {
        List<Map<String, Object>> list=new ArrayList<>();
        for (EnumUserSfYsxzInfo info:EnumUserSfYsxzInfo.values()
        ) {
            String name=info.getName();
            String typeName=info.getTypeName();
            List<String> typeNames=new ArrayList<>();
            String[] split = typeName.split(",");
            typeNames.addAll(Arrays.asList(split));

            Map<String, Object> map=lscreenMapper.getPayUserInfo(typeNames);
            map.put("mc",name);
            list.add(map);
        }

        return list;
    }

    @Override
    public List<Map<String, Object>> getWaterType() {
       return  lscreenMapper.getWaterType();
    }
    @Override
    public List<Map<String, Object>> getSalesWaterCount(Integer month) {
        //近12月生产，售水量统计
        List<Map<String ,Object>> list=new ArrayList<>();
        Date now=new Date();
        String formatter="yyyyMM";
        for (Integer i=0;i<=month;i++){
            String yearAndMonth=getYearAndMonth(now,-i,formatter);
            Map<String ,Object>  map=lscreenMapper.getSalesWaterCount(yearAndMonth);
            map.put("cwrq",yearAndMonth);
            list.add(map);
        }
        return list;

    }

    @Override
    public Map<String, Object> getLastMonthSalesWater() {
        //上月生产，售水量统计
        Date now=new Date();
        String formatter="yyyyMM";
       String yearAndMonth=getYearAndMonth(now,-1,formatter);
       return lscreenMapper.getSalesWaterCount(yearAndMonth);

    }

    @Override
    public Map<String, Object> getUserAndMoneyReport() {
        return lscreenMapper.getUserAndMoneyReport();
    }


    public String getYearAndMonth(Date time, Integer num,String formatter){//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//Date date = format.parse(time);

        Calendar calendar=Calendar.getInstance();

        calendar.setTime(time);

        calendar.add(Calendar.MONTH, num);

        Date newTime=calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(formatter);
        String formatDate = sdf.format(newTime);
        return formatDate;

    }

}
