package com.xinelu.manage.controller.screeningproject;

import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.R;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.utils.uuid.IdUtils;
import com.xinelu.manage.domain.hospitalpersoninfo.HospitalPersonInfo;
import com.xinelu.manage.domain.screeningproject.ScreeningProject;
import com.xinelu.manage.service.hospitalpersoninfo.IHospitalPersonInfoService;
import com.xinelu.manage.service.screeningproject.IScreeningProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 项目控制器
 * @author: haown
 * @create: 2023-01-19 10:49
 **/
@RestController
@RequestMapping("/system/screening/project")
@Api(tags = "项目控制器")
public class ScreeningProjectController extends BaseController {
    @Resource
    private IScreeningProjectService projectService;
	@Resource
	private IHospitalPersonInfoService personInfoService;

    @ApiOperation("服务项目分页列表")
    @GetMapping("list")
    public TableDataInfo list(ScreeningProject project) throws Exception {
	    if (!getLoginUser().getUser().isAdmin()) {
		    HospitalPersonInfo personInfo = personInfoService.selectHospitalPersonInfoById(getLoginUser().getUser().getHospitalPersonId());
		    if (personInfo != null) {
			    project.setHospitalId(personInfo.getHospitalId());
		    } else {
			    throw new Exception("该账号未绑定医生账号");
		    }
	    }
        startPage();
	    List<ScreeningProject> list = projectService.findList(project);
	    return getDataTable(list);
    }

    @ApiOperation("服务项目列表")
    @GetMapping("getList")
    public R<List<ScreeningProject>> getList(ScreeningProject project) throws Exception {
		// TODO 根据角色判断查看数据范围
	    if (!getLoginUser().getUser().isAdmin()) {
		    HospitalPersonInfo personInfo = personInfoService.selectHospitalPersonInfoById(getLoginUser().getUser().getHospitalPersonId());
		    if (personInfo != null) {
			    project.setHospitalId(personInfo.getHospitalId());
		    } else {
			    throw new Exception("该账号未绑定医生账号");
		    }
	    }
	    List<ScreeningProject> list = projectService.findList(project);
	    return R.ok(list);
    }

    @ApiOperation("新增服务项目")
    @PostMapping("add")
    public R<String> add(@RequestBody ScreeningProject project) {
		if (projectService.checkSameProjectName(project.getProjectName(), project.getHospitalId(), null)) {
            return R.fail("【" + project.getHospitalName() + "】已存在服务项目【" + project.getProjectName() + "】，不能重复添加");
        }
        //价格校验
        if (Objects.isNull(project.getDiscount())) {
            project.setDiscount(10);
            if (Objects.isNull(project.getPrice())) {
                project.setPrice(new BigDecimal(0));
            }
            project.setDiscountPrice(project.getPrice());
        }
        boolean greaterThan = Objects.nonNull(project.getDiscount()) && project.getDiscount() > 10;
        boolean lessThan = Objects.nonNull(project.getDiscount()) && project.getDiscount() < 1;
        if (BooleanUtils.isTrue(greaterThan) || BooleanUtils.isTrue(lessThan)) {
            return R.fail("折扣不能大于10,或小于1");
        }
        if (StringUtils.isBlank(project.getStatus())) {
            project.setStatus("1");
        }
        project.setDiscountPrice(project.getPrice().multiply(new BigDecimal(project.getDiscount())).multiply(new BigDecimal("0.1")));
        project.setProjectId(IdUtils.fastSimpleUUID());
        project.setCreateBy(getUsername());
        project.setCreateTime(new Date());
        project.setDelFlag("0");
        return projectService.insert(project) > 0 ? R.ok() : R.fail("保存失败");
    }

    @ApiOperation("修改服务项目")
    @PostMapping("update")
    public R<?> update(@RequestBody ScreeningProject project) {
        if (projectService.checkSameProjectName(project.getProjectName(), project.getHospitalId(), project.getProjectId())) {
            return R.fail("【" + project.getHospitalName() + "】已存在服务项目【" + project.getProjectName() + "】，不能重复添加");
        }
        //价格校验
        if (Objects.isNull(project.getDiscount())) {
            project.setDiscount(10);
            if (Objects.isNull(project.getPrice())) {
                project.setPrice(new BigDecimal(0));
            }
            project.setDiscountPrice(project.getPrice());
        }
        boolean greaterThan = Objects.nonNull(project.getDiscount()) && project.getDiscount() > 10;
        boolean lessThan = Objects.nonNull(project.getDiscount()) && project.getDiscount() < 1;
        if (BooleanUtils.isTrue(greaterThan) || BooleanUtils.isTrue(lessThan)) {
            return R.fail("折扣不能大于10,或小于1");
        }
        if (StringUtils.isBlank(project.getStatus())) {
            project.setStatus("1");
        }
        project.setDiscountPrice(project.getPrice().multiply(new BigDecimal(project.getDiscount())).multiply(new BigDecimal("0.1")));
        project.setUpdateBy(getUsername());
        project.setUpdateTime(new Date());
        projectService.update(project);
        return R.ok();
    }

    @ApiOperation("修改服务项目状态")
    @PostMapping("changeStatus")
    public R<?> changeStatus(@RequestBody ScreeningProject project) {
        project.setUpdateBy(getUsername());
        projectService.update(project);
        return R.ok();
    }

    @ApiOperation("逻辑删除服务项目")
    @PostMapping("delete/{projectId}")
    @ApiImplicitParam(name = "projectId", value = "服务项目编号", required = true, dataTypeClass = String.class)
    public R<?> delete(@PathVariable String projectId) {
	    ScreeningProject project = new ScreeningProject();
        project.setProjectId(projectId);
        project.setDelFlag("1");
        project.setUpdateBy(getUsername());
        projectService.delete(project);
        return R.ok();
    }
}
