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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.ESpecialArea;
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.system.config.BdAreaFreePostage;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.system.config.service.IBdAreaFreePostageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 专区免邮控制器
 */
@RestController
@RequestMapping("/manage/ares-free-postage")
public class BdAreaFreePostageController extends BaseController {

    @Autowired
    private IBdAreaFreePostageService iBdAreaFreePostageService;

    /**
     * 列表
     *
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list() {
        LambdaQueryWrapper<BdAreaFreePostage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAreaFreePostage::getPkCountry, SecurityUtils.getPkCountry());
        queryWrapper.orderByAsc(BdAreaFreePostage::getSpecialArea);
        List<BdAreaFreePostage> list = iBdAreaFreePostageService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            for (BdAreaFreePostage bdAreaFreePostage : list) {
                bdAreaFreePostage.setSpecialAreaVal(ESpecialArea.getLabelByValue(bdAreaFreePostage.getSpecialArea()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 新建
     *
     * @param param
     * @return
     */
    @PostMapping("/create")
    public AjaxResult create(@RequestBody BdAreaFreePostage param) {
        if (null == param.getSpecialArea() || null == param.getFreeAmount() || null == param.getEnableState()) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }

        LambdaQueryWrapper<BdAreaFreePostage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAreaFreePostage::getPkCountry, SecurityUtils.getPkCountry());
        queryWrapper.eq(BdAreaFreePostage::getSpecialArea, param.getSpecialArea());
        BdAreaFreePostage bdAreaFreePostage = iBdAreaFreePostageService.getOne(queryWrapper);
        if (null != bdAreaFreePostage) {
            return AjaxResult.error("专区配置已存在");
        }

        param.setPkCountry(SecurityUtils.getPkCountry());
        param.setPkCreator(SecurityUtils.getUserId());
        param.setCreationTime(new Date());
        return toAjax(iBdAreaFreePostageService.save(param));
    }

    /**
     * 修改
     *
     * @param pkId
     * @param param
     * @return
     */
    @PutMapping("/update/{pkId}")
    public AjaxResult update(@PathVariable("pkId") Integer pkId, @RequestBody BdAreaFreePostage param) {
        if (null == param.getSpecialArea() || null == param.getFreeAmount() || null == param.getEnableState()) {
            // 缺少参数
            return AjaxResult.error(TransactionUtils.getContent(CommonMsgConstants.MISSING_PARAM));
        }
        param.setPkId(pkId);
        param.setPkModified(SecurityUtils.getUserId());
        param.setModifiedTime(new Date());
        return toAjax(iBdAreaFreePostageService.updateById(param));
    }

    /**
     * 查询详情
     *
     * @param pkId
     * @return
     */
    @GetMapping("/detail/{pkId}")
    public AjaxResult detail(@PathVariable("pkId") Integer pkId) {
        return AjaxResult.success(iBdAreaFreePostageService.getById(pkId));
    }

    /**
     * 删除
     *
     * @param pkId
     * @return
     */
    @DeleteMapping("/delete/{pkId}")
    public AjaxResult delete(@PathVariable("pkId") Integer pkId) {
        LambdaUpdateWrapper<BdAreaFreePostage> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(BdAreaFreePostage::getPkId, pkId);
        updateWrapper.set(BdAreaFreePostage::getDelFlag, EDelFlag.DELETE.getValue());
        updateWrapper.set(BdAreaFreePostage::getPkModified, SecurityUtils.getUserId());
        updateWrapper.set(BdAreaFreePostage::getModifiedTime, new Date());
        return toAjax(iBdAreaFreePostageService.update(updateWrapper));
    }

}
