package com.cusc.nirvana.user.rnr.fp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cusc.nirvana.common.result.PageResult;
import com.cusc.nirvana.common.result.Response;
import com.cusc.nirvana.rds.mybatis.PageHelper;
import com.cusc.nirvana.user.eiam.client.OrganizationClient;
import com.cusc.nirvana.user.eiam.dto.OrganizationDTO;
import com.cusc.nirvana.user.rnr.fp.common.ResponseCode;
import com.cusc.nirvana.user.rnr.fp.converter.FpRnrProtocolManageConverter;
import com.cusc.nirvana.user.rnr.fp.dao.FpRnrProtocolManageDao;
import com.cusc.nirvana.user.rnr.fp.dao.entity.FpRnrProtocolManagePO;
import com.cusc.nirvana.user.rnr.fp.dto.FpRnrProtocolManageDTO;
import com.cusc.nirvana.user.rnr.fp.service.IFpRnrProtocolManageService;
import com.cusc.nirvana.user.rnr.fp.service.OrganizationService;
import com.cusc.nirvana.user.rnr.mg.constants.CommonDeleteEnum;
import com.cusc.nirvana.user.util.CuscStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * (FpRnrProtocolManage)表服务实现类
 *
 * @author yuy336
 * @since 2022-06-01 10:52:45
 */
@Service
public class FpRnrProtocolManageServiceImpl extends ServiceImpl<FpRnrProtocolManageDao, FpRnrProtocolManagePO> implements IFpRnrProtocolManageService {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private OrganizationClient organizationClient;
    /**
     * 通过UUID查询单条数据
     *
     * @param bean
     * @return 实例对象
     */
    @Override
    public FpRnrProtocolManageDTO getByUuid(FpRnrProtocolManageDTO bean) {
        FpRnrProtocolManagePO record = this.getPoByUuid(bean.getUuid());;
        return FpRnrProtocolManageConverter.INSTANCE.poToDto(record);
    }
    
     /**
     * 通过查询条件查询集合数据
     *
     * @param bean
     * @return 集合对象
     */
    @Override
    public Response<List<FpRnrProtocolManageDTO>> queryByList(FpRnrProtocolManageDTO bean) {
        OrganizationDTO organizationDTO = new OrganizationDTO();
        organizationDTO.setUuid(bean.getOrgId());
        organizationDTO.setTenantNo(bean.getTenantNo());

        String ordId = "";
        if(!StringUtils.isEmpty(bean.getType())&&"1".equals(bean.getType())){
            organizationDTO.setUuid(null);
            organizationDTO.setParentId("0");
            organizationDTO.setBizType(1);
            Response<List<OrganizationDTO>> response = organizationClient.queryByList(organizationDTO);

            if(!response.isSuccess()){
                return Response.createError(response.getMsg(), response.getCode());
            }
            if(null == response.getData()){
                return Response.createError(ResponseCode.DATA_IS_NULL.getMsg(), ResponseCode.DATA_IS_NULL.getCode());
            }
            ordId = response.getData().get(0).getUuid();

        }else {
            //获取车企组织
            Response<OrganizationDTO> response = organizationService.getCarSubOrganByQueryCode(organizationDTO);
            if(!response.isSuccess()){
                return Response.createError(response.getMsg(), response.getCode());
            }
            ordId = response.getData().getUuid();
        }


        bean.setOrgId(ordId);

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        queryWrapper.eq("org_id",bean.getOrgId());
        queryWrapper.orderByDesc("create_time");
        List<FpRnrProtocolManagePO> record = this.list(queryWrapper);
        return Response.createSuccess(FpRnrProtocolManageConverter.INSTANCE.poListToDtoList(record));
    }

    /**
     * 分页查询
     *
     * @param bean 筛选条件
     * @return 查询结果
     */
    @Override
    public PageResult<FpRnrProtocolManageDTO> queryByPage(FpRnrProtocolManageDTO bean) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("create_time");
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        
       Page<FpRnrProtocolManagePO> page =
                this.page(new Page<>(bean.getCurrPage(), bean.getPageSize()), queryWrapper);
        return PageHelper.convert(page, FpRnrProtocolManageDTO.class);
    }

    /**
     * 新增数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpRnrProtocolManageDTO insert(FpRnrProtocolManageDTO bean) {
       FpRnrProtocolManagePO fpRnrProtocolManagePO = FpRnrProtocolManageConverter.INSTANCE.dtoToPo(bean);
	   fpRnrProtocolManagePO.setUuid(CuscStringUtils.generateUuid());
        this.save(fpRnrProtocolManagePO);
		bean.setUuid(fpRnrProtocolManagePO.getUuid());
        return bean;
    }

    /**
     * 修改数据
     *
     * @param bean 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public FpRnrProtocolManageDTO update(FpRnrProtocolManageDTO bean) {
        FpRnrProtocolManagePO fpRnrProtocolManagePO = this.getPoByUuid(bean.getUuid());
		 if(fpRnrProtocolManagePO == null){
            return null;
        }
		FpRnrProtocolManagePO tmpBean = FpRnrProtocolManageConverter.INSTANCE.dtoToPo(bean);
		tmpBean.setId(fpRnrProtocolManagePO.getId());
        this.updateById(tmpBean);
        return bean;
    }

    /**
     * 通过主键删除数据
     * @param bean 实例对象
     * @return 是否成功
     */
    @Override
    @Transactional
    public boolean deleteById(FpRnrProtocolManageDTO bean) {
        FpRnrProtocolManagePO fpRnrProtocolManagePO = this.getPoByUuid(bean.getUuid());
		 if (fpRnrProtocolManagePO == null) {
            return false;
        }
        FpRnrProtocolManagePO tmpBean = new FpRnrProtocolManagePO();
        tmpBean.setId(fpRnrProtocolManagePO.getId());
        tmpBean.setIsDelete(CommonDeleteEnum.DELETED.getCode());
        return this.updateById(tmpBean);
    }
    
    /**
     * 通过UUID查询单条数据
     *
     * @param uuid
     * @return 实例对象
     */
    private FpRnrProtocolManagePO getPoByUuid(String uuid) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uuid", uuid);
        queryWrapper.eq("is_delete", CommonDeleteEnum.NORMAL.getCode());
        return this.getOne(queryWrapper);
    }
}
