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.entity.Employee;
import com.ym.mec.biz.dal.entity.ReplacementInstrumentCooperation;
import com.ym.mec.biz.dal.page.ReplacementInstrumentCooperationQueryInfo;
import com.ym.mec.biz.service.ReplacementInstrumentCooperationService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import io.swagger.annotations.Api;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RequestMapping("replacementInstrumentCooperation")
@Api(tags = "乐器置换-生成链接管理服务")
@RestController
public class ReplacementInstrumentCooperationController extends BaseController {

    @Autowired
    private ReplacementInstrumentCooperationService replacementInstrumentCooperationService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private EmployeeDao employeeDao;

    @ApiOperation(value = "分页查询列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('replacementInstrumentCooperation/queryPage')")
    public HttpResponseResult<PageInfo<ReplacementInstrumentCooperation>> queryPage(ReplacementInstrumentCooperationQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
        Employee employee = employeeDao.get(sysUser.getId());
        if (StringUtils.isEmpty(queryInfo.getOrganId())) {
            queryInfo.setOrganId(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.getOrganId().split(",")))) {
                return failed("非法请求");
            }
        }
        return succeed(replacementInstrumentCooperationService.getPageList(queryInfo));
    }

    @ApiOperation(value = "新增合作单位置换活动")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('replacementInstrumentCooperation/add')")
    public HttpResponseResult<ReplacementInstrumentCooperation> add(ReplacementInstrumentCooperation replacementInstrumentCooperation) {
        ReplacementInstrumentCooperation hasOld = replacementInstrumentCooperationService.findByCooperationOrganIdAndTopicId(replacementInstrumentCooperation.getCooperationOrganId(),
                replacementInstrumentCooperation.getTopicId());
        if (hasOld != null) {
            return failed("合作单位链接已生成，不能重复生成");
        }
        Date nowDate = new Date();
        replacementInstrumentCooperation.setCreateTime(nowDate);
        replacementInstrumentCooperation.setUpdateTime(nowDate);
        replacementInstrumentCooperationService.insert(replacementInstrumentCooperation);
        return succeed(replacementInstrumentCooperation);
    }

    @ApiOperation(value = "开启缴费")
    @PostMapping("/openPay")
    @PreAuthorize("@pcs.hasPermissions('replacementInstrumentCooperation/openPay')")
    public HttpResponseResult<ReplacementInstrumentCooperation> openPay(Integer id) {
        return succeed(replacementInstrumentCooperationService.openPay(id));
    }

    @ApiOperation(value = "修改")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('replacementInstrumentCooperation/update')")
    public Object update(Integer id,String title) {
        ReplacementInstrumentCooperation cooperation = replacementInstrumentCooperationService.get(id);
        if(cooperation == null){
            return failed("活动不存在");
        }
        cooperation.setTitle(title);
        return succeed(replacementInstrumentCooperationService.update(cooperation));
    }
}
