package vip.xiaonuo.erp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import vip.xiaonuo.erp.entity.ErpBdDepartment;
import vip.xiaonuo.erp.entity.ErpBdOrg;
import vip.xiaonuo.erp.mapper.ErpBdDepartmentMapper;
import vip.xiaonuo.erp.mapper.ErpBdOrgMapper;
import vip.xiaonuo.erp.service.IErpBdOrgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.timer.ErpVoucherTimer;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2025-01-03
 */
@Service
public class ErpBdOrgServiceImpl extends ServiceImpl<ErpBdOrgMapper, ErpBdOrg> implements IErpBdOrgService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ErpBdOrgMapper erpBdOrgMapper;

    @Autowired
    private ErpBdDepartmentMapper erpBdDepartmentMapper;


    @Override
    public void getOrg() {
        ResponseEntity<List<ErpBdOrg>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-bd-org/getOrg",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpBdOrg>>() {}
        );
        List<ErpBdOrg> forObjects = response.getBody();
        for (ErpBdOrg erpBdOrg : forObjects) {
            Integer forgid = erpBdOrg.getForgid();
            ErpBdOrg forgid1 = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", forgid));
            if (ObjectUtil.isEmpty(forgid1)){
                erpBdOrgMapper.insert(erpBdOrg);
            }
        }

    }

    @Override
    public void getDept() {

        ResponseEntity<List<ErpBdDepartment>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-bd-org/getDep",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpBdDepartment>>() {}
        );
        List<ErpBdDepartment> forObjects = response.getBody();
        for (ErpBdDepartment erpBdOrg : forObjects) {
            Integer forgid = erpBdOrg.getFdeptid();
            ErpBdDepartment forgid1 = erpBdDepartmentMapper.selectOne(new QueryWrapper<ErpBdDepartment>().eq("fdeptid", forgid));
            if (ObjectUtil.isEmpty(forgid1)){
                Integer fcreateorgid = erpBdOrg.getFcreateorgid();
                ErpBdOrg forgid2 = erpBdOrgMapper.selectOne(new QueryWrapper<ErpBdOrg>().eq("forgid", fcreateorgid));
                if (ObjectUtil.isEmpty(forgid2)){
                    System.out.println("组织不存在");
                }
                erpBdOrg.setOrgId(forgid2.getOrgId());
                erpBdDepartmentMapper.insert(erpBdOrg);
            }
        }

    }

    @Override
    public void getShouRuJinE() {

        ResponseEntity<List<ErpVoucherTimer>> response = restTemplate.exchange(
                "http://127.0.0.1:9200/erp-voucher/getShouRuJinE",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference<List<ErpVoucherTimer>>() {}
        );

        System.out.println(response.getBody());
    }
}
