package yangtz.cs.liu.wechat.controller.equipment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yangtz.cs.liu.campus.domain.schoolEquipment.SchoolCirculation;
import yangtz.cs.liu.campus.domain.schoolEquipment.SchoolEquipmentLedger;
import yangtz.cs.liu.campus.domain.schoolEquipment.SchoolEquipmentRepair;
import yangtz.cs.liu.campus.domain.schoolEquipment.SchoolReceive;
import yangtz.cs.liu.campus.service.equipment.SchoolReceiveService;
import yangtz.cs.liu.campus.service.schoolEquipment.ICirculationService;
import yangtz.cs.liu.campus.service.schoolEquipment.IEquipmentLedgerService;
import yangtz.cs.liu.campus.service.schoolEquipment.IEquipmentRepairService;
import yangtz.cs.liu.campus.vo.schoolEquipment.SchoolCirculationVo;
import yangtz.cs.liu.campus.vo.schoolEquipment.SchoolEquipmentRepairVo;
import yangtz.cs.liu.campus.vo.schoolEquipment.SchoolReceiveVo;

/**
 * 移动端，设备租借、领用
 */
@RestController
@RequestMapping("/wx/equipment")
public class EquipmentLeaseController {

  @Autowired
  IEquipmentLedgerService iEquipmentService;


  @Autowired
  IEquipmentRepairService iEquipmentRepairService;

  @Autowired
  ICirculationService iCirculationService;

  @Autowired
  SchoolReceiveService schoolReceiveService;

  @Autowired
  ISysUserService iSysUserService;

  /**
   * 扫码借用获取设备基本信息
   */
  @GetMapping("/getEquipment/{encode}")
  private AjaxResult getEquipment(@PathVariable("encode") String encode){
    LambdaQueryWrapper<SchoolEquipmentLedger> wrapper =  new LambdaQueryWrapper<>();
    wrapper.eq(SchoolEquipmentLedger::getEncode,encode);
    SchoolEquipmentLedger one = iEquipmentService.getOne(wrapper);
    return AjaxResult.success(one);
  }

  /**
   * 我的借用记录
   */
  @GetMapping("/getMyBorrowList/{borrowById}")
  private AjaxResult getMyBorrowList(@PathVariable("borrowById") Long borrowById){
    SchoolCirculationVo schoolCirculationVo = new SchoolCirculationVo();
    schoolCirculationVo.setBorrowById(borrowById);
    List<SchoolCirculationVo> schoolCirculationVos = iCirculationService
        .selectSchoolCirculationVoList(schoolCirculationVo);
    return AjaxResult.success(schoolCirculationVos);
  }

  /**
   * 扫码借用
   */
  @PostMapping("/addRecord")
  private AjaxResult addRecord(@RequestBody SchoolCirculation schoolCirculation){
    Date borrowTime = schoolCirculation.getBorrowTime();
    Long deadline = schoolCirculation.getDeadline();
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(borrowTime);
    calendar.add(Calendar.DATE,Integer.valueOf(deadline.toString()));
    Date targetDate = calendar.getTime();
    schoolCirculation.setReturnState("0");
    schoolCirculation.setReturnTime(targetDate);
    boolean save = iCirculationService.save(schoolCirculation);
   //修改设备表借出状态
    SchoolEquipmentLedger byId = iEquipmentService.getById(schoolCirculation.getEquipmentId());
    byId.setIsLend("1");
    iEquipmentService.updateById(byId);
    return AjaxResult.success(save);
  }

  /**
   * 扫码借用查看
   */
  @GetMapping("/byCirculation/{id}")
  private AjaxResult byCirculation(@PathVariable("id") Long id){
    SchoolCirculationVo byId = iCirculationService.getSchoolCirculationVoById(id);
    return AjaxResult.success(byId);
  }

  /**
   * 领用台账list
   */
  @GetMapping("/getUseList/{recipientById}")
  private AjaxResult getMyBorrowList(@PathVariable("recipientById") String recipientById){

    List<SchoolReceiveVo> recipientByIdlist = schoolReceiveService
        .getRecipientByIdlist(recipientById);
    return AjaxResult.success(recipientByIdlist);
  }

  /**
   * 领用台账查询
   */
  @GetMapping("/getUseById/{Id}")
  private AjaxResult getUseById(@PathVariable("Id") Long Id){
    SchoolReceiveVo byId = schoolReceiveService.getRecipientById(Id);
    return AjaxResult.success(byId);
  }

  /**
   * 新增领用
   */
  @PostMapping("/addUsedevice")
  private AjaxResult addUsedevice(@RequestBody SchoolReceive schoolReceive){
    boolean save = schoolReceiveService.save(schoolReceive);
    return AjaxResult.success(save);
  }

  /**
   * 设备维修-列表
   */
  @GetMapping("/getMaintenanceList/{reportRepairid}")
  private AjaxResult getMaintenanceList(@PathVariable("reportRepairid") Long reportRepairid){
    Boolean flag= false;
    //1.管理员可以查看所有维修列表
    SysUser sysUser = iSysUserService.selectUserById(reportRepairid);
    List<SysRole> roles = sysUser.getRoles();
       for(SysRole data :roles){
      if (data.getRoleKey().contains("admin") || data.getRoleKey().contains("productCategoryAdmin")){
          flag=true;
         break;
      }
    };
    if (false){
      List<SchoolEquipmentRepairVo> getlist = iEquipmentRepairService.getlist();
      return AjaxResult.success(getlist);
    }else {
      List<SchoolEquipmentRepairVo> maintenanceList = iEquipmentRepairService
          .getMaintenanceList(reportRepairid);
      return AjaxResult.success(maintenanceList);
    }
  }

  /**
   * 设备维修-新增
   */
  @PostMapping("/addMaintenance")
  private AjaxResult addMaintenance(@RequestBody SchoolEquipmentRepair schoolEquipmentRepair){
    boolean save = iEquipmentRepairService.save(schoolEquipmentRepair);
    return AjaxResult.success(save);
  }

  /**
   * 设备报修台账查看
   */
  @GetMapping("/getMaintenance/{id}")
  private AjaxResult getMaintenance(@PathVariable("id") Long id){
    SchoolEquipmentRepairVo equipmentById = iEquipmentRepairService.getEquipmentById(id);
    return AjaxResult.success(equipmentById);
  }
  /**
   * 设备报修修改
   */
  @PutMapping("/pudateMaintenance")
  private AjaxResult pudateMaintenance(@RequestBody SchoolEquipmentRepair schoolEquipmentRepair){
    boolean save = iEquipmentRepairService.updateById(schoolEquipmentRepair);
    return AjaxResult.success(save);
  }

  /**
   * 设备报修删除
   */
  @DeleteMapping("/deletEuipemnt/{id}")
  private AjaxResult deletEuipemnt(@PathVariable("id") Long id){
    boolean b = iEquipmentRepairService.removeById(id);
    return AjaxResult.success(b);
  }
  /**
   * 报修台账反馈列表 设备维修记录id
   */
  @GetMapping("/getfeedback/{id}")
  private AjaxResult getfeedback(@PathVariable("id") Long id){
    SchoolEquipmentRepairVo equipmentById = iEquipmentRepairService.getEquipmentById(id);
    return AjaxResult.success(equipmentById);
  }

  /**
   * 报修反馈提交
   */
  @PutMapping("/pudateFeedback")
  private AjaxResult pudateFeedback(@RequestBody SchoolEquipmentRepair schoolEquipmentRepair){
    boolean b = iEquipmentRepairService.updateById(schoolEquipmentRepair);
    return AjaxResult.success(b);
  }

  /**
   * 维修人员更改维修状态
   */
  @PutMapping("/confirmRepair")
  private AjaxResult confirmRepair(@RequestBody SchoolEquipmentRepair schoolEquipmentRepair){
    boolean b = iEquipmentRepairService.updateById(schoolEquipmentRepair);
    return AjaxResult.success(b);
  }

  /**
   * 维修人员报修列表
   */
  @GetMapping("getMaintenanceUserList")
  private AjaxResult getMaintenanceUserList(){
    List<SchoolEquipmentRepairVo> maintenanceUserList = iEquipmentRepairService
        .getMaintenanceUserList();
    return AjaxResult.success(maintenanceUserList);
  }

  /**
   * 管理员列表查询
   */
  @GetMapping("/getReturningEquipmentList/{userId}")
  private AjaxResult getReturningEquipmentList(@PathVariable("userId") Long userId){
    //根据管理员信息查看所有已借出设备信息
    List<SchoolCirculationVo> returningEquipmentByadminId = iCirculationService
        .getReturningEquipmentByadminId(userId);
    return AjaxResult.success(returningEquipmentByadminId);
  }

  /**
   * 管理员查看详情
   */
  @GetMapping("/getReturningEquipmentByList/{id}")
  private AjaxResult getReturningEquipmentByList(@PathVariable("id") Long id){
    //根据管理员信息查看信息详情
    return AjaxResult.success(iCirculationService.getById(id));
  }

  /**
   * 管理员归还操作
   */
  @PutMapping("returnDevice")
  private AjaxResult returnDevice(@RequestBody SchoolCirculation schoolCirculation){
    schoolCirculation.setReturnState("1");
    boolean b = iCirculationService.updateById(schoolCirculation);
    //修改设备表借出状态
    SchoolEquipmentLedger byId = iEquipmentService.getById(schoolCirculation.getEquipmentId());
    byId.setIsLend("0");
    iEquipmentService.updateById(byId);
    return AjaxResult.success(b);
  }




  /**
   * 获取修理人下拉框
   * @return
   */
  @GetMapping("/getRepair")
  public AjaxResult getRepair(){
    return AjaxResult.success(iEquipmentRepairService.getRepair());
  }

}
