package com.lightinit.hsdataplatform.impl.admin;


import com.lightinit.hsdataplatform.dictionary.DicResourceState;
import com.lightinit.hsdataplatform.dictionary.DicResourceType;
import com.lightinit.hsdataplatform.entity.*;
import com.lightinit.hsdataplatform.mapper.ServiceProviderMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.ServiceProviderDataModel;
import com.lightinit.hsdataplatform.service.admin.IServiceProvider;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lightinit.hsdataplatform.model.admin.ServiceProviderSearchModel;

import java.util.Date;
import java.util.List;

@Service
public class ServiceProviderImpl implements IServiceProvider {
    @Autowired
    private ServiceProviderMapper serviceProviderMapper;
    @Override
    public List<ServiceProvider> queryList(ServiceProviderSearchModel serviceProviderSearchModel, BaseExample.Page page) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ServiceProviderExample example=new ServiceProviderExample();
        ServiceProviderExample.Criteria criteria=example.createCriteria();
        if(serviceProviderSearchModel!=null) {
            if (StringUtils.isNotEmpty(serviceProviderSearchModel.getUserName())){
                criteria.andUsernameLike("%" + serviceProviderSearchModel.getUserName() + "%");
            }
            if (StringUtils.isNotEmpty(serviceProviderSearchModel.getProviderName())){
                criteria.andProviderNameLike("%" + serviceProviderSearchModel.getProviderName() + "%");
            }
            if (StringUtils.isNotEmpty(serviceProviderSearchModel.getPhone())){
                criteria.andPhoneLike("%" + serviceProviderSearchModel.getPhone() + "%");
            }
        }

        example.setPage(page);
        return serviceProviderMapper.selectByExample(example);
    }

    @Override
    public long queryCount(ServiceProviderSearchModel serviceProviderSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ServiceProviderExample example=new ServiceProviderExample();
        ServiceProviderExample.Criteria criteria=example.createCriteria();
        if(serviceProviderSearchModel!=null) {
            if (StringUtils.isNotEmpty(serviceProviderSearchModel.getUserName())){
                criteria.andUsernameLike("%" + serviceProviderSearchModel.getUserName() + "%");
            }
            if (StringUtils.isNotEmpty(serviceProviderSearchModel.getProviderName())){
                criteria.andProviderNameLike("%" + serviceProviderSearchModel.getProviderName() + "%");
            }
            if (StringUtils.isNotEmpty(serviceProviderSearchModel.getPhone())){
                criteria.andPhoneLike("%" + serviceProviderSearchModel.getPhone() + "%");
            }
        }
        return serviceProviderMapper.countByExample(example);
    }

    public ResultState<Long> add(ServiceProviderDataModel inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        ServiceProvider entity = new ServiceProvider();
        BeanUtils.copyProperties(inputModel, entity);
        long sqlResult = serviceProviderMapper.insert(entity);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }

    public ResultState<Long> delete(long id) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ServiceProviderExample example=new ServiceProviderExample();
        ServiceProviderExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(id);
        if (!(serviceProviderMapper.deleteByExample(example)>0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据不存在");
        }
        resultState.setData(id);
        return resultState;
    }

    public ServiceProvider queryOne(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return serviceProviderMapper.selectByPrimaryKey(id);
    }

    public ResultState<Long> edit(ServiceProviderDataModel inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        ServiceProviderExample example=new ServiceProviderExample();
        ServiceProviderExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(inputModel.getId());
        ServiceProvider entity = new ServiceProvider();
        BeanUtils.copyProperties(inputModel, entity);
        long sqlResult = serviceProviderMapper.updateByExampleSelective(entity, example);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据更新失败");
        }
        resultState.setData(entity.getId());
        return resultState;
    }

    public ServiceProvider detail(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return serviceProviderMapper.selectByPrimaryKey(id);
    }
}
