package com.jojubanking.boot.module.system.controller.admin.mzdm;

import com.jojubanking.boot.module.system.controller.admin.xytjjg.vo.XytjjgExcelVO;
import com.jojubanking.boot.module.system.controller.admin.xytjjg.vo.XytjjgExportReqVO;
import com.jojubanking.boot.module.system.convert.xytjjg.XytjjgConvert;
import com.jojubanking.boot.module.system.dal.dataobject.xytjjg.XytjjgDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.jojubanking.boot.framework.common.pojo.PageResult;
import com.jojubanking.boot.framework.common.pojo.CommonResult;
import static com.jojubanking.boot.framework.common.pojo.CommonResult.success;

import com.jojubanking.boot.framework.excel.core.util.ExcelUtils;

import com.jojubanking.boot.framework.operatelog.core.annotations.OperateLog;
import static com.jojubanking.boot.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.jojubanking.boot.module.system.controller.admin.mzdm.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.mzdm.MzdmDO;
import com.jojubanking.boot.module.system.convert.mzdm.MzdmConvert;
import com.jojubanking.boot.module.system.service.mzdm.MzdmService;

@Api(tags = "管理后台 - 民族代码")
@RestController
@RequestMapping("/system/mzdm")
@Validated
public class MzdmController {

    @Resource
    private MzdmService mzdmService;

    @PostMapping("/create")
    @ApiOperation("创建民族代码")
    @PreAuthorize("@ss.hasPermission('system:mzdm:create')")
    public CommonResult<Long> createMzdm(@Valid @RequestBody MzdmCreateReqVO createReqVO) {
        return success(mzdmService.createMzdm(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新民族代码")
    @PreAuthorize("@ss.hasPermission('system:mzdm:update')")
    public CommonResult<Boolean> updateMzdm(@Valid @RequestBody MzdmUpdateReqVO updateReqVO) {
        mzdmService.updateMzdm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除民族代码")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:mzdm:delete')")
    public CommonResult<Boolean> deleteMzdm(@RequestParam("id") Long id) {
        mzdmService.deleteMzdm(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得民族代码")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:mzdm:query')")
    public CommonResult<MzdmRespVO> getMzdm(@RequestParam("id") Long id) {
        MzdmDO mzdm = mzdmService.getMzdm(id);
        return success(MzdmConvert.INSTANCE.convert(mzdm));
    }

    @GetMapping("/list")
    @ApiOperation("获得民族代码列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:mzdm:query')")
    public CommonResult<List<MzdmRespVO>> getMzdmList(@RequestParam("ids") Collection<Long> ids) {
        List<MzdmDO> list = mzdmService.getMzdmList(ids);
        return success(MzdmConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得民族代码分页")
    @PreAuthorize("@ss.hasPermission('system:mzdm:query')")
    public CommonResult<PageResult<MzdmRespVO>> getMzdmPage(@Valid MzdmPageReqVO pageVO) {
        PageResult<MzdmDO> pageResult = mzdmService.getMzdmPage(pageVO);
        return success(MzdmConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出民族代码 Excel")
    @PreAuthorize("@ss.hasPermission('system:mzdm:export')")
    @OperateLog(type = EXPORT)
    public void exportMzdmExcel(@Valid MzdmExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MzdmDO> list = mzdmService.getMzdmList(exportReqVO);
        // 导出 Excel
        List<MzdmExcelVO> datas = MzdmConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "民族代码.xls", "数据", MzdmExcelVO.class, datas);
    }

    @GetMapping("/getall")
    @ApiOperation("导出民族代码 Excel")
    @PreAuthorize("@ss.hasPermission('system:mzdm:export')")
    public CommonResult<List<MzdmExcelVO>> getall(@RequestParam("value") String value )  {

        MzdmExportReqVO exportReqVO = new MzdmExportReqVO();
        List<MzdmDO> list = mzdmService.getMzdmList01(exportReqVO);
        // 导出 Excel
        List<MzdmExcelVO> datas = MzdmConvert.INSTANCE.convertList02(list);
        return success(datas);
    }

}
