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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.constant.msg.CommonMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
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;

/**
 * @Description: 专区免邮控制器
 * @Author: jiang chao
 * @Time: 2023/11/30 15:44
 * @Classname: BdAreaFreePostageController
 * @PackageName: com.hzs.system.config.controller.manage
 */
@RestController
@RequestMapping("/manage/ares-free-postage")
public class BdAreaFreePostageController extends BaseController {

    @Autowired
    private IBdAreaFreePostageService iBdAreaFreePostageService;

    /**
     * 列表
     *
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list() {
        QueryWrapper<BdAreaFreePostage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, SecurityUtils.getPkCountry());
        queryWrapper.orderByAsc("SPECIAL_AREA");
        return getDataTable(iBdAreaFreePostageService.list(queryWrapper));
    }

    /**
     * 新建
     *
     * @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));
        }

        QueryWrapper<BdAreaFreePostage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, SecurityUtils.getPkCountry());
        queryWrapper.eq("SPECIAL_AREA", 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) {
        UpdateWrapper<BdAreaFreePostage> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq(SystemFieldConstants.PK_ID, pkId);
        updateWrapper.set(SystemFieldConstants.DEL_FLAG, EDelFlag.DELETE.getValue());
        updateWrapper.set(SystemFieldConstants.PK_MODIFIED, SecurityUtils.getUserId());
        updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
        return toAjax(iBdAreaFreePostageService.update(updateWrapper));
    }

}
