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.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
import com.hzs.common.core.utils.StringUtils;
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.BdAwardsDetail;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdAwardsDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 奖衔配置明细明细表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-09-13
 */
@Slf4j
@RestController
@RequestMapping("/manage/awards-detail")
public class BdAwardsDetailController extends BaseController {
    @Autowired
    private IBdAwardsDetailService awardsDetailService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    /**
     * @description: 查询奖衔配置明细列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [awardsDetail]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.AWARDS_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdAwardsDetail awardsDetail) {
        startPage();
        QueryWrapper<BdAwardsDetail> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if(StringUtils.isNotNull(awardsDetail.getRelationType())){
            queryWrapper.eq("RELATION_TYPE",awardsDetail.getRelationType());
        }
        queryWrapper.orderByAsc("CREATION_TIME");
        List<BdAwardsDetail> list = awardsDetailService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增奖衔配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [awardsDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AWARDS_DETAIL, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdAwardsDetail awardsDetail) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        awardsDetail.setPkCountry(pkCountry);
        if(StringUtils.isNull(awardsDetail.getPkAwards())){
            return AjaxResult.error(ConfigMsgConstants.AWARDS_NAME_NOT_NULL);
        }
        if(StringUtils.isNull(awardsDetail.getRelationType())){
            return AjaxResult.error(ConfigMsgConstants.RELATION_TYPE_NOT_NULL);
        }
        if(StringUtils.isNull(awardsDetail.getPkCheckAwards())){
            return AjaxResult.error(ConfigMsgConstants.PK_CHECK_AWARDS_NOT_NULL);
        }
        if(StringUtils.isNull(awardsDetail.getAwardsValue())){
            return AjaxResult.error(ConfigMsgConstants.AWARDS_VALUE_NOT_NULL);
        }

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


    /**
     * @description: 修改奖衔配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [awardsDetail]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AWARDS_DETAIL, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdAwardsDetail awardsDetail) {
        Integer pkCountry=SecurityUtils.getPkCountry();
        awardsDetail.setPkCountry(pkCountry);
        if(StringUtils.isNull(awardsDetail.getPkAwards())){
            return AjaxResult.error(ConfigMsgConstants.AWARDS_NAME_NOT_NULL);
        }
        if(StringUtils.isNull(awardsDetail.getRelationType())){
            return AjaxResult.error(ConfigMsgConstants.RELATION_TYPE_NOT_NULL);
        }
        if(StringUtils.isNull(awardsDetail.getPkCheckAwards())){
            return AjaxResult.error(ConfigMsgConstants.PK_CHECK_AWARDS_NOT_NULL);
        }
        if(StringUtils.isNull(awardsDetail.getAwardsValue())){
            return AjaxResult.error(ConfigMsgConstants.AWARDS_VALUE_NOT_NULL);
        }

        awardsDetail.setPkModified(SecurityUtils.getUserId());
        awardsDetail.setModifiedTime(new Date());
        return toAjax(awardsDetailService.updateById(awardsDetail));
    }


    /**
     * @description: 查询单条奖衔配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AWARDS_DETAIL, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdAwardsDetail awardsDetail = awardsDetailService.getById(pkId);
        return AjaxResult.success(awardsDetail);
    }


    /**
     * @description: 删除奖衔配置明细
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.AWARDS_DETAIL, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdAwardsDetail> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        return toAjax(awardsDetailService.update(updateWrapper));
    }
}
