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

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.item.vo.*;
import com.jojubanking.boot.module.system.dal.dataobject.item.ItemDO;
import com.jojubanking.boot.module.system.convert.item.ItemConvert;
import com.jojubanking.boot.module.system.service.item.ItemService;

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

    @Resource
    private ItemService itemService;

    @PostMapping("/create")
    @ApiOperation("创建检查项目")
    @PreAuthorize("@ss.hasPermission('system:item:create')")
    public CommonResult<Long> createItem(@Valid @RequestBody ItemCreateReqVO 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(itemService.createItem(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新检查项目")
    @PreAuthorize("@ss.hasPermission('system:item:update')")
    public CommonResult<Boolean> updateItem(@Valid @RequestBody ItemUpdateReqVO updateReqVO) {
        itemService.updateItem(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @ApiOperation("获得检查项目")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:item:query')")
    public CommonResult<ItemRespVO> getItem(@RequestParam("id") Long id) {
        ItemDO item = itemService.getItem(id);
        return success(ItemConvert.INSTANCE.convert(item));
    }

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

    @GetMapping("/page")
    @ApiOperation("获得检查项目分页")
    @PreAuthorize("@ss.hasPermission('system:item:query')")
    public CommonResult<PageResult<ItemRespVO>> getItemPage(@Valid ItemPageReqVO pageVO) {
        PageResult<ItemDO> pageResult = itemService.getItemPage(pageVO);
        return success(ItemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出检查项目 Excel")
    @PreAuthorize("@ss.hasPermission('system:item:export')")
    @OperateLog(type = EXPORT)
    public void exportItemExcel(@Valid ItemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemDO> list = itemService.getItemList(exportReqVO);
        // 导出 Excel
        List<ItemExcelVO> datas = ItemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "检查项目.xls", "数据", ItemExcelVO.class, datas);
    }

}
