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

import com.jojubanking.boot.module.system.dal.dataobject.item.ItemDO;
import com.jojubanking.boot.module.system.service.item.ItemService;
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.text.DecimalFormat;
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.childitem.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.childitem.ChilditemDO;
import com.jojubanking.boot.module.system.convert.childitem.ChilditemConvert;
import com.jojubanking.boot.module.system.service.childitem.ChilditemService;

@Api(tags = "管理后台 - 检查项目子")
@RestController
@RequestMapping("/system/childitem")
@Validated
public class ChilditemController {

    @Resource
    private ChilditemService childitemService;
    @Resource
    private ItemService itemService;

    @PostMapping("/create")
    @ApiOperation("创建检查项目子")
    @PreAuthorize("@ss.hasPermission('system:childitem:create')")
    public CommonResult<Long> createChilditem(@Valid @RequestBody ChilditemCreateReqVO createReqVO) {
        DecimalFormat df = new DecimalFormat("#");
        Double number = Double.parseDouble(createReqVO.getItemprice())*100;
        createReqVO.setItemprice(df.format(number));

        if(createReqVO.getItemflag() == 2){
            //临床
            createReqVO.setZxks(itemService.getLCzxks(createReqVO.getItemname()).get("zxks").toString());
            createReqVO.setZxksname(itemService.getLCzxks(createReqVO.getItemname()).get("zxksname").toString());
        }else{
            //小项目
            createReqVO.setZxks(itemService.getSFXMMzxks(createReqVO.getItemname()).get("zxks").toString());
            createReqVO.setZxksname(itemService.getSFXMMzxks(createReqVO.getItemname()).get("zxksname").toString());
        }
        //开单科室固定的
        createReqVO.setKdks("1071");
        createReqVO.setKdksname("健康管理科");

        return success(childitemService.createChilditem(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新检查项目子")
    @PreAuthorize("@ss.hasPermission('system:childitem:update')")
    public CommonResult<Boolean> updateChilditem(@Valid @RequestBody ChilditemUpdateReqVO updateReqVO) {
        childitemService.updateChilditem(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得检查项目子")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:childitem:query')")
    public CommonResult<ChilditemRespVO> getChilditem(@RequestParam("id") Long id) {
        ChilditemDO childitem = childitemService.getChilditem(id);
        return success(ChilditemConvert.INSTANCE.convert(childitem));
    }

    @GetMapping("/list")
    @ApiOperation("获得检查项目子列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('system:childitem:query')")
    public CommonResult<List<ChilditemRespVO>> getChilditemList(@RequestParam("ids") Collection<Long> ids) {
        List<ChilditemDO> list = childitemService.getChilditemList(ids);
        return success(ChilditemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得检查项目子分页")
    @PreAuthorize("@ss.hasPermission('system:childitem:query')")
    public CommonResult<PageResult<ChilditemRespVO>> getChilditemPage(@Valid ChilditemPageReqVO pageVO) {
        PageResult<ChilditemDO> pageResult = childitemService.getChilditemPage(pageVO);
        return success(ChilditemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出检查项目子 Excel")
    @PreAuthorize("@ss.hasPermission('system:childitem:export')")
    @OperateLog(type = EXPORT)
    public void exportChilditemExcel(@Valid ChilditemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ChilditemDO> list = childitemService.getChilditemList(exportReqVO);
        // 导出 Excel
        List<ChilditemExcelVO> datas = ChilditemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "检查项目子.xls", "数据", ChilditemExcelVO.class, datas);
    }

}
