package com.gree.mall.manager.logic.workorder;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.listvo.workorder.OrderSmallTypeVO;
import com.gree.mall.manager.commonmapper.CommonMapper;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.OrderSmallType;
import com.gree.mall.manager.plus.service.*;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class OrderSmallTypeLogic {

    private final CommonLogic commonLogic;
    private final OrderSmallTypeService orderSmallTypeService;
    private final CommonMapper commonMapper;

    /**
     * 工单类型列表
     * @param zfireParamBean
     * @return
     */
    public IPage<OrderSmallTypeVO> orderSmallTypeList(ZfireParamBean zfireParamBean){
        FieldUtils.supplyParam(zfireParamBean,OrderSmallTypeVO.class,commonLogic.getAdminUser());
        IPage<OrderSmallTypeVO> page = commonMapper.orderSmallTypeList(new Page(zfireParamBean.getPageNum(), zfireParamBean.getPageSize()), zfireParamBean);
        return page;
    }


    /**
     * 保存工单类型
     * @param orderSmallType
     */
    public void save(OrderSmallType orderSmallType){
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if(StringUtils.isAnyBlank(orderSmallType.getOrderType(),orderSmallType.getOrderSmallTypeText())){
            throw new RemoteServiceException("请填写必要的参数");
        }
        Integer count = orderSmallTypeService.lambdaQuery()
                .ne(StringUtils.isNotBlank(orderSmallType.getId()), OrderSmallType::getId, orderSmallType.getId())
                .eq(OrderSmallType::getOrderSmallTypeText, orderSmallType.getOrderSmallTypeText())
                .eq(OrderSmallType::getCompanyWechatId,adminUser.getCompanyWechatId())
                .count();
        if(count > 0){
            throw new RemoteServiceException("请勿录入重复的名称");
        }
        orderSmallType.setCompanyWechatId(adminUser.getCompanyWechatId());
        orderSmallType.setCompanyWechatName(adminUser.getCompanyName());
        orderSmallType.insertOrUpdate();
    }

    /**
     * 工单小类详情
     * @param id
     * @return
     */
    public OrderSmallType detail(String id){
        OrderSmallType orderSmallType = orderSmallTypeService.getById(id);
        return orderSmallType;
    }

    /**
     * 删除工单小类
     * @param id
     */
    public void delete(String id){
        orderSmallTypeService.removeById(id);
    }


}
