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

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.xytjjg.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.xytjjg.XytjjgDO;
import com.jojubanking.boot.module.system.convert.xytjjg.XytjjgConvert;
import com.jojubanking.boot.module.system.service.xytjjg.XytjjgService;

@Api(tags = "管理后台 - HIS项目")
@RestController
@RequestMapping("/system/xytjjg")
@Validated
public class XytjjgController {

    @Resource
    private XytjjgService xytjjgService;

    @PostMapping("/create")
    @ApiOperation("创建HIS项目")
    @PreAuthorize("@ss.hasPermission('system:xytjjg:create')")
    public CommonResult<Long> createXytjjg(@Valid @RequestBody XytjjgCreateReqVO createReqVO) {
        return success(xytjjgService.createXytjjg(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新HIS项目")
    @PreAuthorize("@ss.hasPermission('system:xytjjg:update')")
    public CommonResult<Boolean> updateXytjjg(@Valid @RequestBody XytjjgUpdateReqVO updateReqVO) {
        xytjjgService.updateXytjjg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除HIS项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:xytjjg:delete')")
    public CommonResult<Boolean> deleteXytjjg(@RequestParam("id") Long id) {
        xytjjgService.deleteXytjjg(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得HIS项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:xytjjg:query')")
    public CommonResult<XytjjgRespVO> getXytjjg(@RequestParam("id") Long id) {
        XytjjgDO xytjjg = xytjjgService.getXytjjg(id);
        return success(XytjjgConvert.INSTANCE.convert(xytjjg));
    }

    @GetMapping("/list")
    @ApiOperation("获得HIS项目列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:xytjjg:query')")
    public CommonResult<List<XytjjgRespVO>> getXytjjgList(@RequestParam("ids") Collection<Long> ids) {
        List<XytjjgDO> list = xytjjgService.getXytjjgList(ids);
        return success(XytjjgConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得HIS项目分页")
    @PreAuthorize("@ss.hasPermission('system:xytjjg:query')")
    public CommonResult<PageResult<XytjjgRespVO>> getXytjjgPage(@Valid XytjjgPageReqVO pageVO) {
        PageResult<XytjjgDO> pageResult = xytjjgService.getXytjjgPage(pageVO);
        return success(XytjjgConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出HIS项目 Excel")
    @PreAuthorize("@ss.hasPermission('system:xytjjg:export')")
    @OperateLog(type = EXPORT)
    public void exportXytjjgExcel(@Valid XytjjgExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<XytjjgDO> list = xytjjgService.getXytjjgList(exportReqVO);
        // 导出 Excel
        List<XytjjgExcelVO> datas = XytjjgConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "HIS项目.xls", "数据", XytjjgExcelVO.class, datas);
    }

    @GetMapping("/getall")
    @ApiOperation("导出HIS项目 Excel")
    @PreAuthorize("@ss.hasPermission('system:xytjjg:export')")
    public CommonResult<List<XytjjgHisItems>> getall(@RequestParam("value") String value ){
        //复用excel导出功能，产生前端需要动态数据
        XytjjgExportReqVO exportReqVO = new XytjjgExportReqVO();
        exportReqVO.setItemname(value);
        exportReqVO.setItemno(value);
        List<XytjjgDO> list = xytjjgService.getXytjjgFilterList01(exportReqVO);

        List<XytjjgHisItems> datas = XytjjgConvert.INSTANCE.convertList03(list);

        if(list.size() !=0 ){
            for(int i=0; i<datas.size(); i++){
                datas.get(i).setLabel(datas.get(i).getItemno() + " " + datas.get(i).getItemname());
            }
        }

        return success(datas);
    }

}
