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


import cn.hutool.core.bean.BeanUtil;
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.BdGradeScope;
import com.hzs.common.domain.system.ext.BdGradeScopeExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.param.BdGradeScopeParam;
import com.hzs.system.config.service.IBdGradeScopeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 等级范围 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/manage/bd-grade-scope")
public class BdGradeScopeController extends BaseController {
    @Autowired
    private IBdGradeScopeService gradeScopeService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    /**
     * @description: 查询等级范围配置 列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [gradeScope]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BD_GRADE_SCOPE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list() {
        startPage();
        BdGradeScopeExt gradeScopeExt=new BdGradeScopeExt();
        gradeScopeExt.setPkCountry(SecurityUtils.getPkCountry());
        List<BdGradeScopeExt> list = gradeScopeService.selectGradeScopeList(gradeScopeExt);
        List<BdGradeScopeParam> listParam =new ArrayList<>();
        list.forEach(item -> listParam.add(BeanUtil.copyProperties(item, BdGradeScopeParam.class)));
        return getDataTable(listParam);
    }



    /**
     * @description: 新增等级范围配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [gradeScope]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_GRADE_SCOPE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdGradeScope gradeScope) {
        if(StringUtils.isNull(gradeScope.getPkScope())){
            return AjaxResult.error(ConfigMsgConstants.PK_SCOPE_NOT_NULL);
        }
        gradeScope.setPkCountry(SecurityUtils.getPkCountry());
        gradeScope.setPkCreator(SecurityUtils.getUserId());
        gradeScope.setCreationTime(new Date());

        return toAjax(gradeScopeService.save(gradeScope));
    }


    /**
     * @description: 修改等级范围配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [gradeScope]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_GRADE_SCOPE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdGradeScope gradeScope) {
        if(StringUtils.isNull(gradeScope.getPkScope())){
            return AjaxResult.error(ConfigMsgConstants.PK_SCOPE_NOT_NULL);
        }
        gradeScope.setPkModified(SecurityUtils.getUserId());
        gradeScope.setModifiedTime(new Date());
        return toAjax(gradeScopeService.updateById(gradeScope));
    }


    /**
     * @description: 查询单条等级范围配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_GRADE_SCOPE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdGradeScope grade = gradeScopeService.getById(pkId);
        return AjaxResult.success(grade);
    }


    /**
     * @description: 删除等级范围配置
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BD_GRADE_SCOPE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdGradeScope> 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(gradeScopeService.update(updateWrapper));
    }
}
