package com.hzs.sale.deliver.controller.manage;

import com.hzs.common.core.annotation.AccessPermissions;
import com.hzs.common.core.constant.CountryConstants;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.deliver.param.*;
import com.hzs.sale.deliver.vo.DeliverSecondVO;
import com.hzs.sale.order.service.ISaOrderService;
import com.hzs.sale.util.OrderUtil;
import com.hzs.system.base.IAreaServiceApi;
import com.hzs.system.sys.IUserServiceApi;
import com.hzs.system.sys.dto.UserAuthorityDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Description: 发货清单二次发货控制器
 */
@RestController
@RequestMapping("/manage/deliver-second")
public class SaDeliverSecondController extends BaseController {

    @Autowired
    private ISaOrderService iSaOrderService;

    @DubboReference
    IAreaServiceApi iAreaServiceApi;
    @DubboReference
    IUserServiceApi iUserServiceApi;

    /**
     * 列表
     *
     * @param param
     * @return
     */
    @AccessPermissions("DeliverySecond")
    @GetMapping("/list")
    public TableDataInfo list(DeliverSecondParam param) {
        param.setCreationTime(DateUtils.handTimeStartAndEnd(param.getCreationTime()));
        param.setPayTime(DateUtils.handTimeStartAndEnd(param.getPayTime()));

        Integer pkCountry = SecurityUtils.getPkCountry();

        // 获取管理员权限（角色地区范围、体系列表、团队列表）
        UserAuthorityDTO userAuthorityDTO = iUserServiceApi.getUserAuthority(SecurityUtils.getUserId()).getData();
        param.setAreaScopeList(userAuthorityDTO.getRoleAreaScopeList());
        param.setVertexIdList(userAuthorityDTO.getVertexIdList());
        param.setTeamList(userAuthorityDTO.getUserTeamList());

        startPage();
        List<DeliverSecondVO> resultList = iSaOrderService.secondList(param, pkCountry);

        // 当查询有数据才进行处理
        if (resultList.size() > 0) {
            // 获取行政区划map
            Map<Integer, String> areaMap = iAreaServiceApi.getAreaMap(pkCountry).getData();

            for (DeliverSecondVO deliverSecond : resultList) {
                // 省市区处理
                OrderUtil.handleOrderAddress(areaMap, deliverSecond);

                if (pkCountry.equals(CountryConstants.CHINA_COUNTRY)) {
                    if (null != deliverSecond.getInExchangeRate()) {
                        deliverSecond.setOrderAchieveRmb(deliverSecond.getOrderAchieve().multiply(deliverSecond.getInExchangeRate()));
                    }
                }
            }
        }

        return getDataTable(resultList);
    }

    /**
     * 查看明细
     *
     * @param pkId 订单ID
     * @return
     */
    @GetMapping("/detail")
    public AjaxResult detail(@RequestParam Long pkId) {
        // 查询订单以及详情
        SaOrderExt saOrderExt = iSaOrderService.queryOrderListByIdList(Collections.singletonList(pkId), SecurityUtils.getPkCountry()).get(0);
        return AjaxResult.success(saOrderExt.getOrderItemsExtList());
    }

}
