package com.lightinit.hsdataplatform.impl.admin;

import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.Partner;
import com.lightinit.hsdataplatform.entity.PartnerExample;
import com.lightinit.hsdataplatform.entity.ServiceProviderExample;
import com.lightinit.hsdataplatform.mapper.PartnerMapper;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.ResultStateCode;
import com.lightinit.hsdataplatform.model.admin.PartnerSearchModel;
import com.lightinit.hsdataplatform.service.admin.IPartnerService;
import com.lightinit.hsdataplatform.spring.DynamicDataContextHolder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class PartnerServiceImpl implements IPartnerService {
    @Autowired
    private PartnerMapper partnerMapper;
    @Override
    public List<Partner> queryList(PartnerSearchModel partnerSearchModel, BaseExample.Page pager) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PartnerExample example=new PartnerExample();
        PartnerExample.Criteria criteria=example.createCriteria();
        if(partnerSearchModel!=null) {
            if (StringUtils.isNotEmpty(partnerSearchModel.getContact())){
                criteria.andContactLike("%" + partnerSearchModel.getContact() + "%");
            }
            if (StringUtils.isNotEmpty(partnerSearchModel.getPartnerName())){
                criteria.andPartnerNameLike("%" + partnerSearchModel.getPartnerName() + "%");
            }
            if (StringUtils.isNotEmpty(partnerSearchModel.getPhone())){
                criteria.andPhoneLike("%" + partnerSearchModel.getPhone() + "%");
            }
        }

        example.setPage(pager);

        return partnerMapper.selectByExample(example);
    }

    @Override
    public long queryCount(PartnerSearchModel partnerSearchModel) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PartnerExample example=new PartnerExample();
        PartnerExample.Criteria criteria=example.createCriteria();
        if(partnerSearchModel!=null) {
            if (StringUtils.isNotEmpty(partnerSearchModel.getContact())){
                criteria.andContactLike("%" + partnerSearchModel.getContact() + "%");
            }
            if (StringUtils.isNotEmpty(partnerSearchModel.getPartnerName())){
                criteria.andPartnerNameLike("%" + partnerSearchModel.getPartnerName() + "%");
            }
            if (StringUtils.isNotEmpty(partnerSearchModel.getPhone())){
                criteria.andPhoneLike("%" + partnerSearchModel.getPhone() + "%");
            }
        }

        return partnerMapper.countByExample(example);
    }

    @Override
    public ResultState add(Partner inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        inputModel.setCreatedAt(new Date());
        long sqlResult = partnerMapper.insert(inputModel);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据插入失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }

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

    @Override
    public Object queryOne(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return partnerMapper.selectByPrimaryKey(id);
    }

    @Override
    public ResultState edit(Partner inputModel) {
        ResultState<Long> resultState = new ResultState<Long>();
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        PartnerExample example=new PartnerExample();
        PartnerExample.Criteria criteria=example.createCriteria();
        criteria.andIdEqualTo(inputModel.getId());
        long sqlResult = partnerMapper.updateByExampleSelective(inputModel, example);
        if (!(sqlResult > 0)){
            resultState.setStateCode(ResultStateCode.INVALID_DATA);
            resultState.setMsg("数据更新失败");
        }
        resultState.setData(inputModel.getId());
        return resultState;
    }

    @Override
    public Object detail(long id) {
        DynamicDataContextHolder.setCustomerType(DynamicDataContextHolder.DEFAULT);
        return partnerMapper.selectByPrimaryKey(id);
    }
}
