package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.dao.EmployeeDao;
import com.ym.mec.biz.dal.dto.MusicGroupSubjectGoodsAndInfoDto;
import com.ym.mec.biz.dal.dto.StudentAddDto;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.entity.SellOrder;
import com.ym.mec.biz.dal.entity.StudentPaymentOrderDetail;
import com.ym.mec.biz.dal.entity.SubjectChange;
import com.ym.mec.biz.dal.enums.SubjectChangeStatusEnum;
import com.ym.mec.biz.dal.page.SellOrderQueryInfo;
import com.ym.mec.biz.dal.page.SubjectChangeQueryInfo;
import com.ym.mec.biz.service.MusicGroupSubjectPlanService;
import com.ym.mec.biz.service.StudentRegistrationService;
import com.ym.mec.biz.service.SubjectChangeService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.util.date.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@RequestMapping("subjectChange")
@Api(tags = "声部更换服务")
@RestController
public class SubjectChangeController extends BaseController {

    @Autowired
    private StudentRegistrationService studentRegistrationService;
    @Autowired
    private MusicGroupSubjectPlanService musicGroupSubjectPlanService;
    @Autowired
    private SubjectChangeService subjectChangeService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;

    @ApiOperation("声部更换服务列表")
    @GetMapping(value = "/queryPage")
    @PreAuthorize("@pcs.hasPermissions('subjectChange/queryPage')")
    public HttpResponseResult<PageInfo<SubjectChange>> queryPage(SubjectChangeQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        if (!sysUser.getIsSuperAdmin()) {
            Employee employee = employeeDao.get(sysUser.getId());
            if (StringUtils.isEmpty(queryInfo.getOrganIdList())) {
                queryInfo.setOrganIdList(employee.getOrganIdList());
            } else if (StringUtils.isEmpty(employee.getOrganIdList())) {
                return failed("用户所在分部异常");
            } else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if (!list.containsAll(Arrays.asList(queryInfo.getOrganIdList().split(",")))) {
                    return failed("非法请求");
                }
            }
        }
        if (queryInfo.getEndTime() != null) {
            queryInfo.setEndTime(DateUtil.getLastTimeWithDay(queryInfo.getEndTime()));
        }
        queryInfo.setStatus(SubjectChangeStatusEnum.SUCCESSED);
        return succeed(subjectChangeService.getPageList(queryInfo));
    }

    @ApiOperation(value = "获取更换前的详情")
    @GetMapping("/getStudentOriginal")
    @PreAuthorize("@pcs.hasPermissions('subjectChange/getStudentOriginal')")
    @ApiImplicitParams({@ApiImplicitParam(name = "studentId", value = "学生编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public HttpResponseResult<SubjectChange> getStudentOriginal(Integer studentId, String musicGroupId) {
        if (studentId == null || StringUtils.isEmpty(musicGroupId)) {
            return failed("参数校验异常");
        }
        return succeed(subjectChangeService.getStudentOriginal(studentId, musicGroupId));
    }

    @ApiOperation(value = "获取乐团声部费用信息及乐器和辅件")
    @GetMapping("/getSubjectGoodsAndInfo")
    @PreAuthorize("@pcs.hasPermissions('subjectChange/getSubjectGoodsAndInfo')")
    @ApiImplicitParams({@ApiImplicitParam(name = "subjectId", value = "声部信息", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "musicGroupId", value = "乐团编号", required = true, dataType = "String")})
    public HttpResponseResult<MusicGroupSubjectGoodsAndInfoDto> getSubjectGoodsAndInfo(Integer subjectId, String musicGroupId) {
        if (subjectId == null || StringUtils.isEmpty(musicGroupId)) {
            return failed("参数校验异常");
        }
        return succeed(musicGroupSubjectPlanService.getSubjectGoodsAndInfo(musicGroupId, subjectId));
    }

    @ApiOperation(value = "添加乐器更换")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('subjectChange/add')")
    public HttpResponseResult<SubjectChange> add(SubjectChange subjectChange) throws Exception {
        return succeed(subjectChangeService.addChange(subjectChange));
    }

    @ApiOperation(value = "取消乐器更换")
    @PostMapping("/cancel")
    @PreAuthorize("@pcs.hasPermissions('subjectChange/cancel')")
    public HttpResponseResult<SubjectChange> cancel(Integer id) throws Exception {
        return succeed(subjectChangeService.cancel(id));
    }

    @ApiOperation(value = "更新成本差")
    @PostMapping("/updateCostMargin")
    @PreAuthorize("@pcs.hasPermissions('subjectChange/updateCostMargin')")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "costMargin", value = "成本差", required = true, dataType = "BigDecimal")})
    public HttpResponseResult<SubjectChange> updateCostMargin(Integer id, BigDecimal costMargin) throws Exception {
        return succeed(subjectChangeService.updateCostMargin(id, costMargin));
    }

    @ApiOperation(value = "获取更换详情")
    @GetMapping("/getChangeInfo")
    @PreAuthorize("@pcs.hasPermissions('subjectChange/getChangeInfo')")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", required = true, dataType = "Integer")})
    public HttpResponseResult<SubjectChange> getChangeInfo(Integer id) {
        if (id == null) {
            return failed("参数校验异常");
        }
        return succeed(subjectChangeService.getChangeInfo(id));
    }
}
