package thyyxxk.webserver.controller.outpatient.wxmall;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import thyyxxk.webserver.config.exception.ExceptionEnum;
import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageCreation;
import thyyxxk.webserver.controller.outpatient.wxmall.dto.PackageInquiry;
import thyyxxk.webserver.entity.ResultVo;
import thyyxxk.webserver.entity.outpatient.wxmall.Type;
import thyyxxk.webserver.entity.outpatient.wxmall.WxmallPackage;
import thyyxxk.webserver.service.outpatient.wxmall.WxmallPackageService;
import thyyxxk.webserver.utils.ResultVoUtil;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/wxmallPackage")
public class WxmallPackageController {

    private final WxmallPackageService service;

    @Autowired
    public WxmallPackageController(WxmallPackageService service) {
        this.service = service;
    }

    @PostMapping("/getPackagesByCondition")
    public ResultVo<List<WxmallPackage>> getPackagesByCondition(@RequestBody PackageInquiry inquiry) {
        List<WxmallPackage> list = service.getPackagesByCondition(inquiry);
        if (null == list || list.isEmpty()) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(list);
    }

    @GetMapping("/getOriginPrice")
    public ResultVo<BigDecimal> getOriginPrice(@RequestParam("type") Type type,
                                               @RequestParam("code") String code) {
        return ResultVoUtil.success(service.getOriginPrice(type, code));
    }

    @PostMapping("/createNewPackage")
    public ResultVo<WxmallPackage> createNewPackage(PackageCreation creation) {
        return ResultVoUtil.success(service.createNewPackage(creation));
    }

    @PostMapping("/createNewPackage2")
    public ResultVo<WxmallPackage> createNewPackage2(@RequestBody PackageCreation creation) {
        return ResultVoUtil.success(service.createNewPackage(creation));
    }

    @PostMapping("/updatePackageState")
    public ResultVo<String> updatePackageState(@RequestBody WxmallPackage pkg) {
        return ResultVoUtil.success(service.updatePackageState(pkg));
    }

    @GetMapping("/getPackageDetail")
    public ResultVo<WxmallPackage> getPackageDetail(@RequestParam("id") String id) {
        WxmallPackage pkg = service.getPackageDetail(id);
        if (null == pkg) {
            return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
        }
        return ResultVoUtil.success(pkg);
    }
}
