package com.njzscloud.dispose.cst.station.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njzscloud.common.mp.support.PageParam;
import com.njzscloud.common.mp.support.PageResult;
import com.njzscloud.dispose.cst.customer.constant.IdentityCategory;
import com.njzscloud.dispose.cst.customer.pojo.param.AddCustomerParam;
import com.njzscloud.dispose.cst.customer.pojo.result.AddCustomerResult;
import com.njzscloud.dispose.cst.customer.service.CustomerService;
import com.njzscloud.dispose.cst.org.constant.OrgCategory;
import com.njzscloud.dispose.cst.org.pojo.param.AddOrgParam;
import com.njzscloud.dispose.cst.station.mapper.StationMapper;
import com.njzscloud.dispose.cst.station.pojo.entity.StationEntity;
import com.njzscloud.dispose.cst.station.pojo.param.AddStationParam;
import com.njzscloud.dispose.cst.station.pojo.param.ModifyStationParam;
import com.njzscloud.dispose.cst.station.pojo.param.StationQueryParam;
import com.njzscloud.dispose.cst.station.pojo.result.StationResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 站点信息
 *
 * @author ljw
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class StationService extends ServiceImpl<StationMapper, StationEntity> implements IService<StationEntity> {

    private final CustomerService customerService;

    @Transactional(rollbackFor = Exception.class)
    public void add(AddStationParam addStationParam) {
        Long userId = addStationParam.getUserId();
        String customerName = addStationParam.getCustomerName();
        String phone = addStationParam.getPhone();
        AddCustomerParam customer = new AddCustomerParam()
            .setUserId(userId)
            .setIdentityCategory(IdentityCategory.XiaoNa)
            .setManager(Boolean.TRUE)
            .setCustomerName(customerName)
            .setPhone(phone)
            .setAddOrgParam(BeanUtil.copyProperties(addStationParam.getOrgInfo(), AddOrgParam.class)
                .setOrgCategory(OrgCategory.QiYe)
            );
        AddCustomerResult customerResult = customerService.add(customer);
        StationEntity stationEntity = BeanUtil.copyProperties(addStationParam, StationEntity.class)
            .setOrgId(customerResult.getOrgId());
        this.save(stationEntity);
    }

    @Transactional(rollbackFor = Exception.class)
    public void modify(ModifyStationParam modifyStationParam) {
        StationEntity stationEntity = BeanUtil.copyProperties(modifyStationParam, StationEntity.class);
        this.updateById(stationEntity);
    }


    public StationResult detail(Long id) {
        return baseMapper.detail(id);
    }

    public PageResult<StationResult> paging(PageParam pageParam, StationQueryParam queryParam) {
        String stationName = queryParam.getStationName();

        QueryWrapper<?> ew = Wrappers.query()
            .eq("a.deleted", 0)
            .and(StrUtil.isNotBlank(stationName), it ->
                it.like("a.station_name", stationName)
                    .or().like("a.address", stationName)
            );

        IPage<StationResult> page = pageParam.<StationResult>toPage()
            .addOrder(OrderItem.desc("a.create_time"), OrderItem.desc("b.create_time"));

        return PageResult.of(baseMapper.paging(page, ew));
    }

}


