package com.om.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.om.entity.po.*;
import com.om.entity.vo.SDKInfoVO;
import com.om.mapper.PcbClientsMapper;
import com.om.mapper.PcbVersionInfoMapper;
import com.om.mapper.SdkVersionInfoMapper;
import com.om.service.IClientService;
import com.om.service.ISdkClientsService;
import com.om.service.ISdkVersionDescribeService;
import com.om.service.ISdkVersionInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.om.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bmmx
 * @since 2024-03-19
 */
@Service
@Slf4j

public class SdkVersionInfoServiceImpl extends ServiceImpl<SdkVersionInfoMapper, SdkVersionInfo> implements ISdkVersionInfoService {
    
    @Resource
    private ISdkClientsService clientsService;
    
    @Resource
    private ISdkVersionDescribeService describeService;
    @Autowired
    private PcbClientsMapper pcbClientsMapper;
    @Autowired
    private PcbVersionInfoMapper pcbVersionInfoMapper;

    @Resource
    private IClientService clientService;


    @Override
    public Result getPageList(Integer pageIndex, Integer pageSize, String username) {
        Page<SdkVersionInfo> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(username), SdkVersionInfo::getAdminName, username)
                .page(new Page<>(pageIndex, pageSize));
        List<SdkVersionInfo> infoList = page.getRecords();
        log.info("infoLost={}",infoList);
        //返回的List集合
        List<SDKInfoVO> sdkInfoVOS = new ArrayList<>();
        for (int i = 0; i < infoList.size(); i++) {
            //传输的类型
            SDKInfoVO sdkInfoVO = new SDKInfoVO();

            SdkVersionInfo sdkVersionInfo = infoList.get(i);
            //先copy基本数据
            BeanUtil.copyProperties(sdkVersionInfo,sdkInfoVO);

            Integer id = sdkVersionInfo.getId();
            //先取语言和描述信息
            LambdaQueryWrapper<SdkVersionDescribe> describeQueryWrapper = new LambdaQueryWrapper<>();
            describeQueryWrapper.eq(SdkVersionDescribe::getSdkId,id);
            List<SdkVersionDescribe> describeList = describeService.list(describeQueryWrapper);
            //把取出来的数据存入俩个字符串中
            String [] l = new String[describeList.size()];
            String [] d = new String[describeList.size()];

            for (int i1 = 0; i1 < describeList.size(); i1++) {
                l[i1] = describeList.get(i1).getLanguage();
                d[i1] = describeList.get(i1).getDescription();
            }
            sdkInfoVO.setDescription(d);
            sdkInfoVO.setLanguage(l);

            //在取出来客户端编号
            LambdaQueryWrapper<SdkClients> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SdkClients::getSdkId,id);
            List<SdkClients> list = clientsService.list(queryWrapper);
            String [] n = new String[list.size()];
            for (int j = 0; j < n.length; j++) {
                String clientNum = list.get(j).getClientNum();
                Client client = clientService.lambdaQuery()
                        .eq(Client::getNumber, clientNum)
                        .one();
                n[j] = client.getName();
            }
            sdkInfoVO.setClientNames(n);
            int lastIndexOf = sdkVersionInfo.getFileName().lastIndexOf(".");
            if (lastIndexOf != -1) {
                sdkInfoVO.setVersionNum(sdkVersionInfo.getFileName().substring(0, lastIndexOf));
            }else {
                sdkInfoVO.setVersionNum("");
            }

            sdkInfoVOS.add(sdkInfoVO);
        }

        Page<SDKInfoVO> sdkVOPage = new Page<>();
        BeanUtil.copyProperties(page,sdkVOPage);
        sdkVOPage.setRecords(sdkInfoVOS);

        return Result.ok(sdkVOPage);
    }

    @Override
    public Result edit(SDKInfoVO infoVO) {
        SdkVersionInfo info = getById(infoVO.getId());
        LambdaQueryWrapper<SdkVersionInfo> infoQueryWapper = new LambdaQueryWrapper<>();
        infoQueryWapper.eq(SdkVersionInfo::getId,info.getId());
        BeanUtil.copyProperties(infoVO,info);
        this.update(info,infoQueryWapper);

        //修改客户端id
        String[] clientNum = infoVO.getClientNum();
        LambdaQueryWrapper<SdkClients> clientQueryWrapper = new LambdaQueryWrapper<>();
        clientQueryWrapper.eq(SdkClients::getSdkId,infoVO.getId());
        clientsService.remove(clientQueryWrapper);
        for (int i = 0; i < clientNum.length; i++) {
            SdkClients SdkClients = new SdkClients();
            SdkClients.setSdkId(infoVO.getId());
            SdkClients.setClientNum(clientNum[i]);
            clientsService.save(SdkClients);
        }

        //没有进行描述和语言进行修改
        String[] language = infoVO.getLanguage();
        String[] description = infoVO.getDescription();
        LambdaQueryWrapper<SdkVersionDescribe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SdkVersionDescribe::getSdkId,infoVO.getId());
        describeService.remove(queryWrapper);
        for (int i = 0; i < language.length; i++) {
            SdkVersionDescribe describe = new SdkVersionDescribe();
            describe.setSdkId(infoVO.getId());
            describe.setDescription(description[i]);
            describe.setLanguage(language[i]);
            describeService.save(describe);
        }

        return Result.ok();

    }

    @Override
    public Result downloadFile(Integer id) {
        return null;
    }

    @Override
    public Result delete(Integer id) {
        boolean sdk = removeById(id);
        LambdaQueryWrapper<SdkVersionDescribe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SdkVersionDescribe::getSdkId,id);
        boolean info = describeService.remove(queryWrapper);
        //还有关联表删除
        LambdaQueryWrapper<SdkClients> clientsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        clientsLambdaQueryWrapper.eq(SdkClients::getSdkId,id);
        clientsService.remove(clientsLambdaQueryWrapper);
        return Result.ok();
    }

    @Override
    public Result add(SDKInfoVO sdkInfoVO) {
        SdkVersionInfo sdkVersionInfo = new SdkVersionInfo();
        BeanUtil.copyProperties(sdkInfoVO,sdkVersionInfo);

        LambdaQueryWrapper<PcbVersionInfo> pcbVersionInforWrapper = new LambdaQueryWrapper<>();
        pcbVersionInforWrapper.eq(PcbVersionInfo::getVersionNum,sdkVersionInfo.getPcbVersion());
        PcbVersionInfo pcbVersionInfo = pcbVersionInfoMapper.selectOne(pcbVersionInforWrapper);
        if (!BeanUtil.isEmpty(pcbVersionInfo)){
            sdkVersionInfo.setPcbId(pcbVersionInfo.getId());
        }
        this.save(sdkVersionInfo);
        Integer sdkInfoId = sdkVersionInfo.getId();
        //SdkClient表关联
        for (int i = 0; i < sdkInfoVO.getClientNum().length; i++) {

            SdkClients sdkClients = new SdkClients();
            sdkClients.setSdkId(sdkInfoId);
            sdkClients.setClientNum(sdkInfoVO.getClientNum()[i]);
            clientsService.save(sdkClients);
        }


        for (int i = 0; i < sdkInfoVO.getLanguage().length; i++) {

            SdkVersionDescribe describe = new SdkVersionDescribe();
            describe.setSdkId(sdkInfoId);
            describe.setLanguage(sdkInfoVO.getLanguage()[i]);
            describe.setDescription(sdkInfoVO.getDescription()[i]);
            describeService.save(describe);
        }

        return  Result.ok();
    }
}
