package cn.com.ty.lift.business.project.controller;

import cn.com.ty.lift.business.capital.dao.entity.model.request.CapitalRepairRequest;
import cn.com.ty.lift.business.capital.service.CapitalRepairService;
import cn.com.ty.lift.business.capital.service.SignInService;
import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.business.library.dao.entity.model.request.LiftExtensionRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.model.CapitalCount;
import cn.com.ty.lift.business.maintenance.dao.entity.model.MaintenanceCount;
import cn.com.ty.lift.business.maintenance.dao.entity.model.request.MaintenanceRecordRequest;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenanceAppResponse;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenanceRecordDetailResponse;
import cn.com.ty.lift.business.maintenance.dao.entity.model.response.MaintenanceRecordResponse;
import cn.com.ty.lift.business.maintenance.service.MaintenanceRecordService;
import cn.com.ty.lift.business.project.dao.entity.model.ProjectAppUser;
import cn.com.ty.lift.business.project.dao.entity.model.request.ProjectRequest;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectCompanyResponse;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectDetailResponse;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectLiftResponse;
import cn.com.ty.lift.business.project.dao.entity.model.response.ProjectUserResponse;
import cn.com.ty.lift.business.project.service.ProjectAppService;
import cn.com.ty.lift.common.verify.Val;
import cn.com.ty.lift.common.verify.Validation;
import cn.com.ty.lift.common.verify.Validator;
import cn.com.xwy.boot.web.dto.RestResponse;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
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;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author bieao
 * @date 2019/12/31
 * @description app端控制层(项目 、 电梯 、 维保 、 大修)
 */
@RestController
@RequestMapping("app")
public class ProjectAppController {

    @Resource
    private ProjectAppService projectAppService;

    @Resource
    private MaintenanceRecordService maintenanceRecordService;

    @Resource
    private SignInService signInService;

    @Resource
    private CapitalRepairService capitalRepairService;

    /**
     * @param request 公司id
     * @return RestResponse 项目列表
     * @description 根据公司id查询公司下项目列表
     * @date 2019/12/31 3:13 PM
     */
    @PostMapping("project/list")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse companyList(@Val @RequestBody ProjectRequest request) {
        IPage<ProjectCompanyResponse> page = projectAppService.companyList(request);
        if (CollUtil.isEmpty(page.getRecords())) {
            return RestResponse.success();
        }
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目id
     * @return RestResponse 维保工列表
     * @description 根据项目id查询项目下维保工
     * @date 2020/1/14 11:25 上午
     */
    @PostMapping("project/user/list")
    @Validation(fields = {"id"})
    public RestResponse userList(@Val @RequestBody ProjectRequest request) {
        List<ProjectAppUser> userList = projectAppService.userList(request);
        if (CollUtil.isEmpty(userList)) {
            return RestResponse.success();
        }
        return RestResponse.success(userList, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request app端新增电梯
     * @return 1.成功, 0.失败, 消息描述
     * @description 新增电梯
     * @date 2020/1/11 4:21 下午
     */
    @PostMapping("lift/add")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse add(@Val @RequestBody LiftExtensionRequest request) {
        Validator.valid(request.getLift(), "registrationCode");
        return projectAppService.add(request);
    }

    /**
     * @param request app端保养信息
     * @return 1.成功, 0.失败, 消息描述
     * @description 新增保养记录
     * @date 2020/1/13 2:12 下午
     */
    @PostMapping("record/add")
    @Validation(fields = {"coordinate", "periods"})
    public RestResponse add(@Val @RequestBody MaintenanceRecordRequest request) {
        Validator.valid(request.getRecord(), "mtPlanId", "liftId", "projectId", "mtCompanyId", "workerId", "type", "position", "workerId1");
        return maintenanceRecordService.saveRecord(request);
    }

    /**
     * @param request app端保养信息
     * @return 1.成功, 0.失败, 消息描述
     * @description 编辑保养记录
     * @date 2020/1/13 2:33 下午
     */
    @PostMapping("record/modify")
    @Validation(fields = {"recordImageList"})
    public RestResponse modify(@Val @RequestBody MaintenanceRecordRequest request) {
        Validator.valid(request.getRecord(), "id", "signatureImg1");
        return maintenanceRecordService.modify(request);
    }

    /**
     * @param request 保养id
     * @return RestResponse 保养记录
     * @description 查看保养记录
     * @date 2020/1/13 2:33 下午
     */
    @Deprecated
    @PostMapping("record/detail")
    @Validation(fields = {"id"})
    public RestResponse detail(@Val @RequestBody MaintenanceRecordRequest request) {
        MaintenanceRecordDetailResponse record = maintenanceRecordService.queryById(request.getId());
        if (ObjectUtil.isEmpty(record)) {
            return RestResponse.success();
        }
        return RestResponse.success(record, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 项目id
     * @return RestResponse 电梯列表
     * @description 根据项目id查询项目下电梯列表
     * @date 2019/12/31 3:13 PM
     */
    @PostMapping("lift/list")
    @Validation(fields = {"id"})
    public RestResponse projectLiftList(@Val @RequestBody ProjectRequest request) {
        IPage<ProjectLiftResponse> page = projectAppService.projectLiftList(request);
        if (CollUtil.isEmpty(page.getRecords())) {
            return RestResponse.success();
        }
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目id
     * @return RestResponse 项目详情
     * @description 根据项目id查询项目详情
     * @date 2020/1/2 1:50 下午
     */
    @PostMapping("project/detail")
    @Validation(fields = {"id"})
    public RestResponse detail(@Val @RequestBody ProjectRequest request) {
        ProjectDetailResponse response = projectAppService.detail(request.getId());
        if (ObjectUtil.isEmpty(response)) {
            return RestResponse.success();
        }
        return RestResponse.success(response, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 用户ID、公司ID
     * @return RestResponse 统计保养数
     * @description 根据用户ID和公司ID统计保养数
     * @date 2020/1/6 11:29 上午
     */
    @PostMapping("maintenance/count")
    @Validation(fields = {"mtCompanyId", "userId", "beginTime"})
    public RestResponse count(@Val @RequestBody MaintenanceRecordRequest request) {
        List<MaintenanceCount> result = projectAppService.count(request);
        return RestResponse.success(result, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 用户ID和维保状态
     * @return RestResponse 保养任务列表
     * @description 根据用户ID和维保状态查询维保计划任务列表
     * @date 2020/1/6 11:29 上午
     */
    @PostMapping("maintenance/planTask")
    @Validation(fields = {"mtCompanyId", "userId", "status"})
    public RestResponse planTask(@Val @RequestBody MaintenanceRecordRequest request) {
        IPage<MaintenanceAppResponse> result = projectAppService.planTask(request);
        return RestResponse.success(result, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 用户id和维保状态
     * @return RestResponse 保养任务列表
     * @description 根据用户id和维保状态查询保养任务列表
     * @date 2020/4/24 5:03 下午
     */
    @PostMapping("maintenance/recordTask")
    @Validation(fields = {"mtCompanyId", "userId", "status"})
    public RestResponse recordTask(@Val @RequestBody MaintenanceRecordRequest request) {
        IPage<MaintenanceAppResponse> result = projectAppService.recordTask(request);
        return RestResponse.success(result, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 公司id和电梯id
     * @return RestResponse 保养信息
     * @description 根据公司id和电梯id查询上次保养信息
     * @date 2020/1/6 5:37 下午
     */
    @PostMapping("maintenance/record/info")
    @Validation(fields = {"mtCompanyId", "liftId"})
    public RestResponse recordInfo(@Val @RequestBody MaintenanceRecordRequest request) {
        MaintenanceRecordResponse response = maintenanceRecordService.recordInfo(request);
        if (ObjectUtil.isEmpty(response)) {
            return RestResponse.success();
        }
        return RestResponse.success(response, MessageUtils.get("msg.query.detail.success"));
    }

    /**
     * @param request 用户签到信息
     * @return 1.成功, 0.失败, 消息描述
     * @description 大修签到
     * @date 2020/2/5 12:45 下午
     */
    @PostMapping("capital/sign/in")
    @Validation(fields = {"mtCompanyId", "projectId", "userId", "coordinate", "address"})
    public RestResponse signIn(@Val @RequestBody CapitalRepairRequest request) {
        return signInService.signIn(request);
    }

    /**
     * @param request 项目id
     * @return Integer 成功1或失败0
     * @description 大修项目开始
     * @date 2020/4/21 3:13 下午
     */
    @PostMapping("capital/begin")
    @Validation(fields = {"projectId"})
    public RestResponse begin(@Val @RequestBody CapitalRepairRequest request) {
        boolean result = capitalRepairService.begin(request);
        if (result) {
            return RestResponse.success(null, MessageUtils.get("msg.modify.success"));
        }
        return RestResponse.fail(MessageUtils.get("msg.modify.fail"));
    }

    /**
     * @param request 项目id，竣工单地址
     * @return Integer 成功1或失败0
     * @description 大修竣工
     * @date 2020/4/26 1:47 下午
     */
    @PostMapping("capital/completed")
    @Validation(fields = {"mtCompanyId", "projectId", "imageUrlList"})
    public RestResponse completed(@Val @RequestBody CapitalRepairRequest request) {
        return capitalRepairService.completed(request);
    }

    /**
     * @param request 逾期原因，逾期时间
     * @return Integer 成功1或失败0
     * @description 逾期申请
     * @date 2020/4/15 11:36 上午
     */
    @PostMapping("capital/overdueApply")
    @Validation(fields = {"mtCompanyId", "projectId", "overdueDate", "overdueReason"})
    public RestResponse overdueApply(@Val @RequestBody CapitalRepairRequest request) {
        boolean result = capitalRepairService.overdueApply(request);
        if (result) {
            return RestResponse.success(null, MessageUtils.get("msg.modify.success"));
        }
        return RestResponse.fail(MessageUtils.get("msg.modify.fail"));
    }

    /**
     * @param request 公司ID，用户ID
     * @return 考勤人员列表
     * @description 考勤人员列表
     * @date 2020/4/15 2:52 下午
     */
    @PostMapping("capital/attendance/list")
    @Validation(fields = {"mtCompanyId", "userId"})
    public RestResponse attendanceList(@Val @RequestBody CapitalRepairRequest request) {
        List<ProjectUserResponse> list = signInService.attendanceList(request);
        return RestResponse.success(list, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 公司id
     * @return RestResponse 项目列表
     * @description 根据公司id查询公司下大修项目列表
     * @date 2020/4/20 8:18 下午
     */
    @PostMapping("capital/project/list")
    @Validation(fields = {"mtCompanyId", "userId", "projectStatus"})
    public RestResponse capitalProjectList(@Val @RequestBody CapitalRepairRequest request) {
        IPage<ProjectCompanyResponse> page = projectAppService.capitalProjectList(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 用户ID、公司ID
     * @return RestResponse 统计大修项目数量
     * @description 根据用户ID和公司ID统计大修项目数量
     * @date 2020/5/29 3:00 下午
     */
    @PostMapping("capital/count")
    @Validation(fields = {"mtCompanyId", "userId"})
    public RestResponse capitalCount(@Val @RequestBody CapitalRepairRequest request) {
        CapitalCount result = projectAppService.countCapitalByUserId(request);
        return RestResponse.success(result, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 大修项目id
     * @return RestResponse 电梯列表
     * @description 根据大修项目id查询项目下电梯列表
     * @date 2020/4/15 4:15 下午
     */
    @PostMapping("capital/lift/list")
    @Validation(fields = {"mtCompanyId", "id"})
    public RestResponse list(@Val @RequestBody CapitalRepairRequest request) {
        IPage<ProjectLiftResponse> page = projectAppService.list(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 项目id
     * @return RestResponse 项目详情
     * @description 根据项目id查询项目详情
     * @date 2020/4/21 9:13 下午
     */
    @PostMapping("capital/project/detail")
    @Validation(fields = {"id", "userId"})
    public RestResponse capitalDetail(@Val @RequestBody CapitalRepairRequest request) {
        ProjectDetailResponse response = projectAppService.capitalDetail(request);
        if (ObjectUtil.isEmpty(response)) {
            return RestResponse.success();
        }
        return RestResponse.success(response, MessageUtils.get("msg.query.detail.success"));
    }
}
