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.PcbInfoVO;
import com.om.mapper.ClientMapper;
import com.om.mapper.PcbVersionInfoMapper;
import com.om.service.IPcbClientsService;
import com.om.service.IPcbVersionDescribeService;
import com.om.service.IPcbVersionInfoService;
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 org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bmmx
 * @since 2024-03-12
 */
@Service
@Slf4j
public class PcbVersionInfoServiceImpl extends ServiceImpl<PcbVersionInfoMapper, PcbVersionInfo> implements IPcbVersionInfoService {

    @Resource
    private IPcbVersionDescribeService describeService;

    @Resource
    private IPcbClientsService pcbClientsService;
    @Autowired
    private ClientMapper clientMapper;

    @Override
    public Result getPageList(Integer pageIndex, Integer pageSize, String username) {
        Page<PcbVersionInfo> page = this.lambdaQuery()
                .like(StringUtils.isNotBlank(username), PcbVersionInfo::getAdminName, username)
                .page(new Page<>(pageIndex, pageSize));
        List<PcbVersionInfo> infoList = page.getRecords();

        //返回的List集合
        List<PcbInfoVO> pcbInfoVOS = new ArrayList<>();
        for (int i = 0; i < infoList.size(); i++) {
            //传输的类型
            PcbInfoVO pcbInfoVO = new PcbInfoVO();

            PcbVersionInfo pcbVersionInfo = infoList.get(i);
            //先copy基本数据
            BeanUtil.copyProperties(pcbVersionInfo,pcbInfoVO);

            Integer id = pcbVersionInfo.getId();
            //先取语言和描述信息
            LambdaQueryWrapper<PcbVersionDescribe> describeQueryWrapper = new LambdaQueryWrapper<>();
            describeQueryWrapper.eq(PcbVersionDescribe::getPcbId,id);
            List<PcbVersionDescribe> 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();
            }
            pcbInfoVO.setDescription(d);
            pcbInfoVO.setLanguage(l);

            //在取出来客户端编号
            LambdaQueryWrapper<PcbClients> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PcbClients::getPcbId,id);
            List<PcbClients> list = pcbClientsService.list(queryWrapper);
            String [] n = new String[list.size()];
            for (int j = 0; j < n.length; j++) {
                String clientNum = list.get(j).getClientNum();
                LambdaQueryWrapper<Client> clientLambdaQueryWrapper = new LambdaQueryWrapper<>();
                clientLambdaQueryWrapper.eq(Client::getNumber,clientNum);
                Client client = clientMapper.selectOne(clientLambdaQueryWrapper);
                if(client!=null){
                    n[j] = client.getName();
                }

            }
            pcbInfoVO.setClientNames(n);
            pcbInfoVOS.add(pcbInfoVO);
        }

        Page<PcbInfoVO> pcbVOPage = new Page<>();
        BeanUtil.copyProperties(page,pcbVOPage);
        pcbVOPage.setRecords(pcbInfoVOS);

        return Result.ok(pcbVOPage);
    }

    @Override
    @Transactional
    public Result edit(PcbInfoVO pcbInfoVO) {
        PcbVersionInfo info = getById(pcbInfoVO.getId());
        LambdaQueryWrapper<PcbVersionInfo> infoQueryWapper = new LambdaQueryWrapper<>();
        infoQueryWapper.eq(PcbVersionInfo::getId,info.getId());
        BeanUtil.copyProperties(pcbInfoVO,info);
        this.update(info,infoQueryWapper);

        //修改客户端id
        String[] clientNum = pcbInfoVO.getClientNum();
        LambdaQueryWrapper<PcbClients> clientQueryWrapper = new LambdaQueryWrapper<>();
        clientQueryWrapper.eq(PcbClients::getPcbId,pcbInfoVO.getId());
        pcbClientsService.remove(clientQueryWrapper);
        for (int i = 0; i < clientNum.length; i++) {
            PcbClients pcbClients = new PcbClients();
            pcbClients.setPcbId(pcbInfoVO.getId());
            pcbClients.setClientNum(clientNum[i]);
            pcbClientsService.save(pcbClients);
        }

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

        return Result.ok();

    }

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

    @Override
    public Result delete(Integer id) {
        boolean pcb = removeById(id);
        LambdaQueryWrapper<PcbVersionDescribe> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PcbVersionDescribe::getPcbId,id);
        boolean info = describeService.remove(queryWrapper);
        //还有关联表删除
        LambdaQueryWrapper<PcbClients> clientsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        clientsLambdaQueryWrapper.eq(PcbClients::getPcbId,id);
        pcbClientsService.remove(clientsLambdaQueryWrapper);
        return Result.ok();
    }

    @Override
    public Result add(PcbInfoVO pcbInfoVO) {
        PcbVersionInfo pcbVersionInfo = new PcbVersionInfo();
        BeanUtil.copyProperties(pcbInfoVO,pcbVersionInfo);
        log.info("-----{}",pcbVersionInfo);
        this.save(pcbVersionInfo);
        Integer pcbInfoId = pcbVersionInfo.getId();

        //PcbClient表关联
        for (int i = 0; i < pcbInfoVO.getClientNum().length; i++) {

            PcbClients pcbClients = new PcbClients();
            pcbClients.setPcbId(pcbInfoId);
            pcbClients.setClientNum(pcbInfoVO.getClientNum()[i]);
            pcbClientsService.save(pcbClients);
        }


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

            PcbVersionDescribe describe = new PcbVersionDescribe();
            describe.setPcbId(pcbInfoId);
            describe.setLanguage(pcbInfoVO.getLanguage()[i]);
            describe.setDescription(pcbInfoVO.getDescription()[i]);
            describeService.save(describe);
        }

        return  Result.ok();
    }
}
