package com.crunii.micro.service.sysmgr.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.exception.BusinessException;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.domain.entity.main.TsmPropertyValue;
import com.crunii.micro.domain.entity.main.YcqRollPic;
import com.crunii.micro.domain.repo.main.YcqRollPicRepo;
import com.crunii.micro.service.sysmgr.api.domain.dto.YcqPageRollPicDTO;
import com.crunii.micro.service.sysmgr.api.domain.dto.YcqQueryRollPicDTO;
import com.crunii.micro.service.sysmgr.api.domain.dto.YcqRollPicDTO;
import com.crunii.micro.service.sysmgr.api.domain.dto.YcqRollPicUrl;
import com.crunii.micro.service.sysmgr.api.domain.vo.YcqRollPicVO;
import com.crunii.micro.service.sysmgr.api.enums.RollPicTypeEnum;
import com.crunii.micro.service.sysmgr.dto.sysmgr.AccessPageVo;
import com.crunii.micro.service.sysmgr.dto.sysmgr.ValueReq;
import com.crunii.micro.service.sysmgr.jdbc.PropertyJdbcDao;
import com.crunii.micro.service.sysmgr.jdbc.YcqRollPicDao;
import com.crunii.micro.service.sysmgr.service.YcqRollPicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 15294
 */
@Service
public class YcqRollPicServiceImpl implements YcqRollPicService {

    @Resource
    private YcqRollPicRepo repo;

    @Resource
    private YcqRollPicDao dao;

    @Resource
    private PropertyJdbcDao propertyJdbcDao;

    @Override
    public YcqRollPicVO queryById(Long id) {
        YcqRollPic one = repo.getOne(id);
        if (null == one){
            throw new BusinessException("no available value");
        }
        YcqRollPicVO ycqRollPicVO = new YcqRollPicVO();
        BeanUtils.copyProperties(one, ycqRollPicVO);
        ycqRollPicVO.setPicUrls(this.convertPic(one.getPicUrls()));
        return ycqRollPicVO;
    }

    @Override
    public PageRsp<YcqRollPicVO> pageQuery(PageReq<YcqPageRollPicDTO> pageRequest) {
        return dao.pageQuery(pageRequest);
    }

    public List<AccessPageVo> getTypeList() {
        return dao.getTypeList();
    }

    @Override
    public Boolean insert(YcqRollPicDTO ycqRollPicDTO, Integer loginId) {

        String location=ycqRollPicDTO.getLocation();
        int count = dao.exist(location,ycqRollPicDTO.getSecondLocation());
        if (count > 0){
            throw new BusinessException("已存在配置,请确认后继续添加!");
        }
        ycqRollPicDTO.setDelFlag(null);
        ycqRollPicDTO.setStatus(null);

        YcqRollPic ycqRollPic = new YcqRollPic();
        BeanUtils.copyProperties(ycqRollPicDTO, ycqRollPic);
        ycqRollPic.setCreateId(loginId);
        if (!CollectionUtils.isEmpty(ycqRollPicDTO.getPicUrls())){
            ycqRollPic.setPicUrls(JSONObject.toJSONString(ycqRollPicDTO.getPicUrls()));
        }
        Date date = new Date();
        ycqRollPic.setCreateTime(date);
        ycqRollPic.setUpdateTime(date);
        repo.save(ycqRollPic);
        return true;
    }

    @Override
    public Boolean update(YcqRollPicDTO ycqRollPicDTO, Integer loginId) {
        String location=ycqRollPicDTO.getLocation();
        String secondLocation=ycqRollPicDTO.getSecondLocation();
        YcqRollPic one = repo.getOne(ycqRollPicDTO.getId());
        if (one == null){
            throw new BusinessException("数据有误,请联系管理员!");
        }
        one.setRollPicName(ycqRollPicDTO.getRollPicName());
        one.setStatus(ycqRollPicDTO.getStatus());
        one.setSecondLocation(secondLocation);
        one.setLocation(location);
        one.setUpdateId(loginId);
        one.setPicUrls(JSONObject.toJSONString(ycqRollPicDTO.getPicUrls()));
        one.setUpdateTime(new Date());
        repo.save(one);
        return true;
    }

    @Override
    public List<YcqRollPicUrl> queryByCondition(YcqQueryRollPicDTO ycqQueryRollPicDTO) {
        YcqRollPic ycqRollPic = dao.queryByCondition(ycqQueryRollPicDTO.getSystemId(), ycqQueryRollPicDTO.getLocationId());
        if (null == ycqRollPic){
            return Collections.emptyList();
        }
        String picUrls = ycqRollPic.getPicUrls();
        if (StringUtils.isBlank(picUrls)){
            return Collections.emptyList();
        }
        return this.convertPic(ycqRollPic.getPicUrls());
    }

    @Override
    public YcqRollPic getYcqRollPicByType(YcqRollPicDTO ycqRollPic) {
        return dao.getYcqRollPicByType(ycqRollPic);
    }

    private List<YcqRollPicUrl> convertPic(String picUrls){
        if (StringUtils.isBlank(picUrls)){
            return Collections.emptyList();
        }
        return JSONObject.parseArray(picUrls, YcqRollPicUrl.class);
    }

    private String getLocation(Integer LocationId){
        ValueReq valueReq = new ValueReq();
        valueReq.setPropCode("imgLocation");
        PageReq<ValueReq> pageRsp = new PageReq<>();
        pageRsp.setQuery(valueReq);
        PageRsp<TsmPropertyValue> tsmPropertyValuePageRsp = propertyJdbcDao.pageValue(pageRsp);
        List<TsmPropertyValue> values = tsmPropertyValuePageRsp.getResult();
        if (CollectionUtils.isEmpty(values)){
            throw new BusinessException("未完善配置信息，请联系管理员");
        }
        List<TsmPropertyValue> collect = values.stream().filter(item -> Objects.equals(item.getValueCode(), String.valueOf(LocationId))).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)){
            throw new BusinessException("未查询到该位置配置，请联系管理员");
        }
        return collect.get(0).getValueName();
    }

    private String getSystemName(Integer systemId){
        ValueReq valueReq = new ValueReq();
        valueReq.setPropCode("imgSystemId");
        PageReq<ValueReq> pageRsp = new PageReq<>();
        pageRsp.setQuery(valueReq);
        PageRsp<TsmPropertyValue> tsmPropertyValuePageRsp2 = propertyJdbcDao.pageValue(pageRsp);
        List<TsmPropertyValue> values2 = tsmPropertyValuePageRsp2.getResult();
            if (CollectionUtils.isEmpty(values2)){
            throw new BusinessException("未完善配置信息，请联系管理员");
        }
        List<TsmPropertyValue> collect2 = values2.stream().filter(item -> Objects.equals(item.getValueCode(), String.valueOf(systemId))).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(collect2)){
            throw new BusinessException("未查询到该系统信息配置，请联系管理员");
        }
        return collect2.get(0).getValueName();
    }
}
