package com.sckw.order.serivce;

import com.github.pagehelper.PageHelper;
import com.sckw.core.model.page.PageResult;
import com.sckw.core.model.vo.TableBottom;
import com.sckw.core.model.vo.TableStatisticRes;
import com.sckw.core.model.vo.TableTop;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.core.utils.StringUtils;
import com.sckw.core.web.context.LoginUserHolder;
import com.sckw.order.dao.KwoTradeOrderMapper;
import com.sckw.order.enums.OrderStatusEnum;
import com.sckw.order.model.dto.OperatorTOrderListStsConditionDTO;
import com.sckw.order.model.dto.OrderListResDTO;
import com.sckw.order.model.dto.TradeOrderListSelectDTO;
import com.sckw.order.model.vo.req.*;
import com.sckw.system.api.RemoteUserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @desc: 运营端贸易订单管理相关service
 * @author: yzc
 * @date: 2023-09-14 11:23
 */
@Service
@Slf4j
@AllArgsConstructor
public class TradeOrderManageService {

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteUserService remoteUserService;

    private final KwoTradeOrderService tradeOrderService;
    private final KwoTradeOrderMapper tradeOrderMapper;


    /**
     * @desc: 贸易订单列表分页查询
     * @author: yzc
     * @date: 2023-09-14 14:24
     * @Param params:
     * @return: com.sckw.core.model.page.PageResult
     */
    public PageResult listPaging(OperatorTOrderListQueryVO params) {
        List<Long> authEntIdList = LoginUserHolder.getAuthEntIdList();
        if (CollectionUtils.isEmpty(authEntIdList)) {
            List<Long> ids = remoteUserService.findEnterpriseIdsByUserIdIsMain(LoginUserHolder.getUserId());
            if (CollectionUtils.isEmpty(ids)) {
                return PageResult.build(params.getPage(), params.getPageSize(), 0L, Collections.emptyList());
            }
            authEntIdList.addAll(ids);
        }

        TradeOrderListStatisticParam tradeOrderListStatisticParam = new TradeOrderListExportParam();
        BeanUtils.copyProperties(params, tradeOrderListStatisticParam);
        TradeOrderListSelectDTO tradeOrderListSelectDTO = tradeOrderService.buildSelectParam(tradeOrderListStatisticParam);
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<OrderListResDTO> list = tradeOrderMapper.tradeOrderSelect(tradeOrderListSelectDTO, tradeOrderListSelectDTO.getGoodIds(), LoginUserHolder.getAuthUserIdList(), authEntIdList);
        return tradeOrderService.buildResult(list, params.getPageSize());
    }

    /**
     * @desc: 贸易订单列表统计查询
     * @author: yzc
     * @date: 2023-09-14 14:22
     * @Param params:
     * @return: com.sckw.core.model.vo.TableStatisticRes
     */
    public TableStatisticRes listStatistic(OperatorTOrderStsQueryVO params) {
        List<Long> authEntIdList = LoginUserHolder.getAuthEntIdList();
        if (CollectionUtils.isEmpty(authEntIdList)) {
            List<Long> ids = remoteUserService.findEnterpriseIdsByUserIdIsMain(LoginUserHolder.getUserId());
            if (CollectionUtils.isEmpty(ids)) {
                List<OrderStatusEnum> enums = OrderStatusEnum.getSortList();
                enums.remove(0);
                List<TableTop> list = new ArrayList<>();
                for (OrderStatusEnum e : enums) {
                    Integer value = e.getCode();
                    TableTop tableTop = new TableTop();
                    tableTop.setName(e.getMsg()).setValue(value).setTotal(0);
                    list.add(tableTop);
                }
                TableTop all = new TableTop();
                all.setName("全部").setTotal(0);
                list.add(0, all);
                TableBottom tableBottom = new TableBottom();
                tableBottom.setTotal(0);
                TableStatisticRes res = new TableStatisticRes();
                res.setTableTops(list).setTableBottom(tableBottom);
                return res;
            }
            authEntIdList.addAll(ids);
        }
        List<OrderStatusEnum> enums = OrderStatusEnum.getSortList();
        enums.remove(0);
        return null;
    }

    /**
     * @desc: 贸易订单列表导出
     * @author: yzc
     * @date: 2023-09-14 14:40
     * @Param params:
     * @return: java.util.List<com.sckw.report.service.param.TradeOrderListExport>
     */
    public List<TradeOrderListExport> export(OperatorTOrderExportQueryVO params) {
        List<Long> authEntIdList = LoginUserHolder.getAuthEntIdList();
        if (CollectionUtils.isEmpty(authEntIdList)) {
            List<Long> ids = remoteUserService.findEnterpriseIdsByUserIdIsMain(LoginUserHolder.getUserId());
            if (CollectionUtils.isEmpty(ids)) {
                List<TradeOrderListExport> exports = new ArrayList<>();
                return exports;
            }
            authEntIdList.addAll(ids);
        }
        return null;
    }

    /**
     * @desc: 合同关联订单列表
     * @author: yzc
     * @date: 2023-09-14 14:52
     * @Param params:
     * @return: com.sckw.core.model.page.PageResult
     */
    public PageResult contractAssociationList(OperatorTOrderContractParam params) {
        Criteria criteria = new Criteria();
        List<Long> ids = StringUtils.splitStrToList(params.getContractIds(), ",", Long.class);
        criteria.and("contractId").in(ids).and("delFlag").is(0);
        Sort sort = Sort.by(Sort.Direction.DESC, "contractId", "createTime");
        return null;
    }
}
