package com.hzs.system.config.controller.manage;

import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.EDelivery;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.system.config.BdDelivery;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.IBdDeliveryService;
import com.hzs.system.config.vo.DeliveryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * Description: 发货方式 控制器
 * Author: jiang chao
 * Time: 2022/10/11 13:52
 * Classname: BdDeliveryController
 * PackageName: com.hzs.system.config.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/manage/delivery")
public class BdDeliveryController extends BaseController {

    @Autowired
    private IBdDeliveryService iBdDeliveryService;

    /**
     * 列表
     *
     * @return
     */
    @Log(module = EOperationModule.SYSTEM_CONFIG, business = EOperationBusiness.TRANSPORT_TYPE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public AjaxResult list() {
        // 最终返回结果
        List<DeliveryVO> resultList = new ArrayList<>();
        // 查询已配置列表
        List<BdDelivery> list = iBdDeliveryService.queryList(SecurityUtils.getPkCountry());

        for (EDelivery value : EDelivery.values()) {
            // 创建最终返回内容
            DeliveryVO tmpDelivery = DeliveryVO.builder()
                    .deliveryValue(value.getValue())
                    .build();
            resultList.add(tmpDelivery);

            // 如果数据库中已经存在，则返回标记为存在
            for (BdDelivery delivery : list) {
                if (value.getValue() == delivery.getValue()) {
                    tmpDelivery.setPkId(delivery.getPkId());
                    break;
                }
            }
        }
        return AjaxResult.success(resultList);
    }

    /**
     * 保存
     *
     * @param deliveryValueList 发货方式值列表
     * @return
     */
    @Log(module = EOperationModule.SYSTEM_CONFIG, business = EOperationBusiness.TRANSPORT_TYPE, method = EOperationMethod.CONFIRM)
    @PutMapping("/save")
    public AjaxResult save(@RequestBody List<Integer> deliveryValueList) {
        iBdDeliveryService.saveData(deliveryValueList, SecurityUtils.getUserId(), SecurityUtils.getPkCountry());
        return AjaxResult.success();
    }

}
