package com.fkzy.warn.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fkzy.warn.common.constants.EntityConstants;
import com.fkzy.warn.mapper.EnterpriseInfoMapper;
import com.fkzy.warn.model.Administrator;
import com.fkzy.warn.model.EnterpriseInfo;
import com.fkzy.warn.model.LawCase;
import com.fkzy.warn.service.AdministratorService;
import com.fkzy.warn.service.EnterpriseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangjing
 * @date 2023/10/16 18:17
 * @description
 */
@Service
public class EnterpriseInfoServiceImpl extends ServiceImpl<EnterpriseInfoMapper, EnterpriseInfo> implements
    EnterpriseInfoService {

    @Autowired
    AdministratorService administratorService;


    @Override
    public void saveModel(EnterpriseInfo entity, String ticket) {
        save(entity);
    }

    @Override
    public void updateModel(EnterpriseInfo entity,String ticket) {
        this.updateById(entity);
    }

    @Override
    public void deleteModel(List<String> ids) {
        EnterpriseInfo entity = new EnterpriseInfo();
        entity.setIsDel(EntityConstants.DEL);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("id", ids);
        update(entity, wrapper);
    }

    @Override
    public void industryModelSave(JSONObject jsonObject) {
        JSONObject data = jsonObject.getJSONObject("data");
        JSONObject BASIC = data.getJSONObject("BASIC");
        JSONArray  PERSON = data.getJSONArray("PERSON");
        EnterpriseInfo enterpriseInfo = JSON.parseObject(BASIC.toString(), EnterpriseInfo.class);
        List<Administrator> administratorList = JSON.parseArray(PERSON.toString(), Administrator.class);
        for (Administrator administrator : administratorList){
            administrator.setCREDITCODE(enterpriseInfo.getCREDITCODE());
        }
        QueryWrapper query = new QueryWrapper<>();
        query.eq("CREDITCODE", enterpriseInfo.getCREDITCODE());
        administratorService.remove(query);
        administratorService.saveBatch(administratorList);
        saveOrUpdate(enterpriseInfo);
    }


}
