package com.mortals.xhx.module.device.service.impl;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.PageInfo;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.dept.DeptPdu;
import com.mortals.xhx.feign.dept.IDeptFeign;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceMatterDatumQuery;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.matter.model.MatterDatumEntity;
import com.mortals.xhx.module.matter.model.MatterDatumQuery;
import com.mortals.xhx.module.matter.model.MatterEntity;
import com.mortals.xhx.module.matter.service.MatterDatumService;
import com.mortals.xhx.module.matter.service.MatterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.xhx.module.device.dao.DeviceMatterDatumDao;
import com.mortals.xhx.module.device.model.DeviceMatterDatumEntity;
import com.mortals.xhx.module.device.service.DeviceMatterDatumService;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
* DeviceMatterDatumService
* 设备事项申请材料业务 service实现
*
* @author zxfei
* @date 2023-02-25
*/
@Service("deviceMatterDatumService")
public class DeviceMatterDatumServiceImpl extends AbstractCRUDServiceImpl<DeviceMatterDatumDao, DeviceMatterDatumEntity, Long> implements DeviceMatterDatumService {

    @Autowired
    private MatterService matterService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private MatterDatumService matterDatumService;

    @Autowired
    private IDeptFeign deptFeign;


    @Override
    protected void findAfter(DeviceMatterDatumEntity params, PageInfo pageInfo, Context context, List<DeviceMatterDatumEntity> list) throws AppException {
        //super.findAfter(params, pageInfo, context, list);
        list.forEach(item->{
            List<MatterDatumEntity> matterDatumEntities = matterDatumService.find(new MatterDatumQuery().matterId(item.getMatterId()));
            if(!ObjectUtils.isEmpty(matterDatumEntities)){
                item.setMatterDatumList(matterDatumEntities);
            }else{
                item.setMatterDatumList(new ArrayList<>());
            }
        });
    }

    @Override
    protected void saveBefore(DeviceMatterDatumEntity entity, Context context) throws AppException {
        super.saveBefore(entity, context);
        int count = this.getDao().getCount(new DeviceMatterDatumQuery().deviceId(entity.getDeviceId()).matterId(entity.getMatterId()));
        if(count>0){
            throw new AppException("当前设备关联事项已存在，不能新增！");
        }

        if(!ObjectUtils.isEmpty(entity.getMatterId())){
            MatterEntity matterEntity = matterService.get(entity.getMatterId());
            if(!ObjectUtils.isEmpty(matterEntity)){
                entity.setMatterCode(matterEntity.getMatterNo());
                entity.setMatterName(matterEntity.getMatterName());
                entity.setMateriaFullName(matterEntity.getMatterFullName());
                entity.setIsRecommend(matterEntity.getIsRecommend());
                entity.setSource(matterEntity.getSource());
                entity.setSort(matterEntity.getSort());
                entity.setDeptId(matterEntity.getDeptId());

                DeptPdu deptPdu = new DeptPdu();
                deptPdu.setDeptNumber(matterEntity.getDeptCode());
                deptPdu.setSiteId(entity.getSiteId());
                Rest<RespData<List<DeptPdu>>> rest = deptFeign.list(deptPdu);
                if(YesNoEnum.YES.getValue()==rest.getCode()){
                    List<DeptPdu> data = rest.getData().getData();
                    if(!ObjectUtils.isEmpty(data)){
                        entity.setDeptId(data.get(0).getId());
                    }
                }

                entity.setDeptCode(matterEntity.getDeptCode());
                entity.setDeptName(matterEntity.getDeptName());
            }
        }

        if(!ObjectUtils.isEmpty(entity.getDeviceId())){
            DeviceEntity deviceEntity = deviceService.get(entity.getDeviceId());
            if(!ObjectUtils.isEmpty(deviceEntity)){
                entity.setDeviceCode(deviceEntity.getDeviceCode());
                entity.setDeviceName(deviceEntity.getDeviceName());
            }
        }

    }


    @Override
    protected void updateBefore(DeviceMatterDatumEntity entity, Context context) throws AppException {
        super.updateBefore(entity, context);
    }
}