package cn.superdesk.uniorder.dubbo.provider.operationCenter.business;

import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.dao.entity.operationCenter.OperationCenterBusiness;
import cn.superdesk.uniorder.dto.param.operationCenter.business.BusinessDeleteParam;
import cn.superdesk.uniorder.dto.param.operationCenter.business.BusinessQueryParam;
import cn.superdesk.uniorder.dto.param.operationCenter.business.BusinessSaveParam;
import cn.superdesk.uniorder.dto.vo.operationCenter.base.OperationCenterVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.business.BusinessCustomerVo;
import cn.superdesk.uniorder.dto.vo.operationCenter.business.BusinessVo;
import cn.superdesk.uniorder.dto.vo.sys.SysSelectVo;
import cn.superdesk.uniorder.dubbo.facade.operationCenter.business.DubboOperationCenterBusinessService;
import cn.superdesk.uniorder.service.operationCenter.OperationCenterBusinessService;
import cn.superdesk.uniorder.service.operationCenter.OperationCenterCustomerService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

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

@Slf4j
@Service(version = "1.0.1", retries = -1, timeout = 10000)
@RequiredArgsConstructor
public class DubboOperationCenterBusinessServiceImpl implements DubboOperationCenterBusinessService {
    private final OperationCenterBusinessService operationCenterBusinessService;
    @Override
    public R<String> saveBusiness(BusinessSaveParam param) {
        if(null == param.getId()){
            //新增
            OperationCenterBusiness business = new OperationCenterBusiness();
            BeanUtils.copyProperties(param, business);
            business.setCreateTime(new Date());
            business.setCreateUserId(param.getOperatorId());
            //由于PC端界面始终需要展示更新时间，因此新增时也塞修改时间的值
            business.setUpdateTime(new Date());
            business.setUpdateUserId(param.getOperatorId());
            business.setEnabled(1);
            business.setDeleted(0);
            operationCenterBusinessService.insertSelective(business);
            return new R<>("新增成功");
        }
        OperationCenterBusiness business = operationCenterBusinessService.findById(param.getId());
        if(null == business){
            return  new R("500", "业务人员不存在，无法修改",false );
        }
        BeanUtils.copyProperties(param, business);
        business.setUpdateTime(new Date());
        business.setUpdateUserId(param.getOperatorId());
        operationCenterBusinessService.updateByPrimaryKeySelective(business);
        return new R<>("修改成功");
    }

    @Override
    public R<String> delete(BusinessDeleteParam param) {
        OperationCenterBusiness business = operationCenterBusinessService.findById(param.getId());
        if(null == business){
            return  new R("500", "业务人员不存在，无法删除",false );
        }
        business.setDeleted(1);
        business.setUpdateTime(new Date());
        business.setUpdateUserId(param.getOperatorId());
        operationCenterBusinessService.updateByPrimaryKeySelective(business);
        return new R<>("删除成功");
    }

    @Override
    public R<BusinessVo> getById(Long id) {
        OperationCenterBusiness business = operationCenterBusinessService.findById(id);
        if(null == business){
            return  new R("500", "业务人员不存在",false );
        }
        BusinessVo vo = new BusinessVo();
        BeanUtils.copyProperties(business,vo);
        return new R<>(vo);
    }

    @Override
    public R<List<BusinessVo>> getListByParams(BusinessQueryParam param) {
        PageHelper.startPage(param.getPageNumber(), param.getPageSize());
        List<BusinessVo> list = operationCenterBusinessService.getListByParams(param);
        PageInfo<BusinessVo> page = new PageInfo<>(list);
        return new R<List<BusinessVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), list);
    }

    @Override
    public R<List<BusinessCustomerVo>> getByBusinessId(BusinessQueryParam param) {
        PageHelper.startPage(param.getPageNumber(), param.getPageSize());
        List<BusinessCustomerVo> list = operationCenterBusinessService.getByBusinessId(param);
        PageInfo<BusinessCustomerVo> page = new PageInfo<>(list);
        return new R<List<BusinessCustomerVo>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), list);
    }

    @Override
    public R<List<OperationCenterVo>> selectAll() {
        return new R<>(operationCenterBusinessService.selectAll());
    }

    @Override
    public List<SysSelectVo> selectList(Map<String, Object> params) {
        return operationCenterBusinessService.selectList(params);
    }
}
