package org.nl.acs.plc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.nl.acs.common.base.PageInfo;
import org.nl.acs.common.base.QueryHelpMybatisPlus;
import org.nl.acs.common.base.impl.CommonServiceImpl;
import org.nl.acs.utils.ConvertUtil;
import org.nl.acs.utils.PageUtil;
import org.nl.acs.opc.domain.OpcPlc;
import org.nl.acs.plc.service.OpcPlcService;
import org.nl.acs.opc.service.dto.OpcPlcDto;
import org.nl.acs.opc.service.dto.OpcPlcQueryParam;
import org.nl.acs.opc.service.mapper.OpcPlcMapper;
import org.nl.system.service.logicflow.dao.StageImage;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author jiaolm
* @date 2023-05-09
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = OpcPlcService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class OpcPlcServiceImpl extends CommonServiceImpl<OpcPlcMapper, OpcPlc> implements OpcPlcService {

    // private final RedisUtils redisUtils;
    private final OpcPlcMapper opcPlcMapper;

    @Override
    public PageInfo<OpcPlcDto> queryAll(OpcPlcQueryParam query, Pageable pageable) {
        IPage<OpcPlc> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<OpcPlc> page = opcPlcMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, OpcPlcDto.class);
    }

    @Override
    public List<OpcPlcDto> queryAll(OpcPlcQueryParam query){
        return ConvertUtil.convertList(opcPlcMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), OpcPlcDto.class);
    }

    @Override
    public OpcPlc getById(String id) {
        return opcPlcMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public OpcPlcDto findById(String id) {
        return ConvertUtil.convert(getById(id), OpcPlcDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(OpcPlcDto resources) {
        OpcPlc entity = ConvertUtil.convert(resources, OpcPlc.class);
        return opcPlcMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(OpcPlcDto resources){
        OpcPlc entity = ConvertUtil.convert(resources, OpcPlc.class);
        int ret = opcPlcMapper.updateById(entity);
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<String> ids){
        // delCaches(ids);
        return opcPlcMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(String id){
        Set<String> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    @Override
    public List<OpcPlc> selectList() {
        List<OpcPlc> lists = opcPlcMapper.selectList(new LambdaQueryWrapper<OpcPlc>()
                .eq(OpcPlc::getIs_delete, "0")
                .eq(OpcPlc::getIs_active, "1")
                .orderByDesc(OpcPlc::getUpdate_time));
        return lists;
    }

    @Override
    public List<OpcPlc> selectListByOpcID(String opc_uuid) {
        //OPC_PLC表【acs_opc_plc】
//        JSONArray arr = WQLObject.getWQLObject("acs_opc_plc").query("is_delete= '0' AND is_active= '1' and opc_server_id = '" + opc_uuid + "'").getResultJSONArray(0);
//        JSONArray result = new JSONArray();
//        for (int i = 0; i < arr.size(); i++) {
//            JSONObject obj = arr.getJSONObject(i);
//            JSONObject json = new JSONObject();
//            json.put("plc_id", obj.getString("plc_id"));
//            json.put("plc_code", obj.getString("plc_code"));
//            json.put("plc_name", obj.getString("plc_name"));
//            result.add(json);
//        }
//        return result;

        List<OpcPlc> lists = opcPlcMapper.selectList(new LambdaQueryWrapper<OpcPlc>()
                .eq(OpcPlc::getIs_delete, "0")
                .eq(OpcPlc::getIs_active, "1")
                .eq(OpcPlc::getOpc_server_id, opc_uuid)
                .orderByDesc(OpcPlc::getUpdate_time));
        return lists;

    }


    /*
    private void delCaches(String id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<String> ids) {
        for (String id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<OpcPlcDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (OpcPlcDto opcPlc : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("plc编码", opcPlc.getPlcCode());
              map.put("plc名字", opcPlc.getPlcName());
              map.put("plc地址", opcPlc.getPlcHost());
              map.put("opcServer标识", opcPlc.getOpcServerId());
              map.put("备注", opcPlc.getRemark());
              map.put("是否启用", opcPlc.getIsActive());
              map.put("是否删除", opcPlc.getIsDelete());
              map.put("创建者", opcPlc.getCreateBy());
              map.put("创建时间", opcPlc.getCreateTime());
              map.put("修改者", opcPlc.getUpdateBy());
              map.put("修改时间", opcPlc.getUpdateTime());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
