package com.gyee.runeconomy.service.update;/*
@author   谢生杰
@date   2023/5/16-10:10
*/

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gyee.common.util.CommonUtils;
import com.gyee.common.util.MD5Utils;
import com.gyee.runeconomy.model.auto.ProBasicPowerstation;
import com.gyee.runeconomy.model.auto.ProBasicUser;
import com.gyee.runeconomy.model.auto.ProEconTargetdata;
import com.gyee.runeconomy.service.auto.IProBasicUserService;
import com.gyee.runeconomy.service.auto.IProEconTargetdataService;
import com.gyee.runeconomy.util.DateUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class TargetEnterService {
    @Resource
    private IProBasicUserService proBasicUserService;
    @Resource
    private IProEconTargetdataService proEconTargetdataService;

    @Value("#{${appendSation.appendSationMap}}")
    private Map<String, String> appendSationMap;

    public ProBasicUser login(ProBasicUser user) {

        String md5Str = MD5Utils.getMD5Str(user.getPassword());

        QueryWrapper<ProBasicUser> query = new QueryWrapper<>();
        query.lambda().eq(ProBasicUser::getUsername, user.getUsername()).eq(ProBasicUser::getPassword, md5Str).eq(ProBasicUser::getIsOpen, 1);
        Optional<ProBasicUser> first = proBasicUserService.list(query).stream().findFirst();
        if (first.isPresent()) {
            return first.get();
        } else {
            return null;
        }
    }

    public boolean save(ProEconTargetdata data) {
        data.setId(CommonUtils.getUUID());
        return proEconTargetdataService.save(data);
    }

    public boolean update(ProEconTargetdata data) {
        return proEconTargetdataService.saveOrUpdate(data);
    }

    public boolean delete(String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        return proEconTargetdataService.removeByIds(list);
    }

    public List<ProEconTargetdata> get(String wpid, String date) {
        Date date1 = DateUtils.pareDateNoHours(date);
        QueryWrapper<ProEconTargetdata> query = new QueryWrapper<>();

        query.lambda().eq(ProEconTargetdata::getRecordDate, date1);
        if (!wpid.equals("0")) {
            query.lambda().eq(ProEconTargetdata::getWindpowerstationId, wpid);
        }
        List<ProEconTargetdata> list = proEconTargetdataService.list(query);
        return list;
    }

    public List<ProBasicPowerstation> wpls(String wpid) {
        List<ProBasicPowerstation> wpls = new ArrayList<>();
        //        if (!wpid.equals("0")){
        //            return wpls.stream().filter(wp->wp.getId().equals(wpid)).collect(Collectors.toList());
        //        }else {
        //            return wpls;
        //        }

        if (!wpid.equals("0")) {
            if (appendSationMap.containsKey(wpid)) {
                ProBasicPowerstation wp = new ProBasicPowerstation();
                wp.setNemCode(wpid);
                wp.setName(appendSationMap.get(wpid));
                wpls.add(wp);
            } else {
                ProBasicPowerstation wp = new ProBasicPowerstation();
                wp.setNemCode(wpid);
                wp.setName("");
                wpls.add(wp);
            }
        } else {
            for (Map.Entry<String, String> entry : appendSationMap.entrySet()) {

                ProBasicPowerstation wp = new ProBasicPowerstation();
                wp.setNemCode(entry.getKey());
                wp.setName(entry.getValue());
                wpls.add(wp);

            }
        }
        return wpls;
    }
}
