package com.repair.controller;


import com.repair.api.RepairWorkTypeControllerAPI;
import com.repair.common.utils.paramUtils;
import com.repair.model.enumModel.eUserZZ;
import com.repair.model.pojo.RepairWorkType;
import com.repair.model.result.CommonResult;
import com.repair.model.result.PageUtils;
import com.repair.model.vo.RepairWorkTypeVo;
import com.repair.services.RepairWorkTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ceshi
 * @since 2023-07-18
 */
@RestController
public class RepairWorkTypeController implements RepairWorkTypeControllerAPI {

    @Autowired
    private RepairWorkTypeService repairWorkTypeService;

    /**
     * 新增工种数据
     * @param rwt 工种数据
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult InsertRepairWorkType(RepairWorkType rwt, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        int result = 0;
        try {
            result = repairWorkTypeService.insertRepairWorkType(rwt);
        } catch (Exception e) {
            if (e.getCause().getMessage().contains("'repair_work_type.name_unique'")) {
                return CommonResult.fail("该工种类型已存在！");
            }
            return CommonResult.fail("系统异常，添加失败！");
        }

        return result > 0 ? CommonResult.ok("添加成功") : CommonResult.fail("添加失败");
    }

    /**
     * 更新工种数据
     * @param rwt 工种数据
     * @param bindingResult 是否为空判断
     * @return
     */
    @Override
    public CommonResult updateRepairWorkTypeById(RepairWorkType rwt, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        if (rwt.getId() == null) {
            return CommonResult.fail("数据ID不能为空");
        }
        int result = 0;
        try {
            result = repairWorkTypeService.updateRepairWorkType(rwt);
        } catch (Exception e) {
            if (e.getCause().getMessage().contains("'repair_work_type.name_unique'")) {
                return CommonResult.fail("该工种类型已存在！");
            }
            return CommonResult.fail("系统异常，添加失败！");
        }

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }

    /**
     * 工种分页数据查询
     * @param currentPage 当前页数
     * @param pageCount 一页数据条数
     * @param name 查询名称
     * @return
     */
    @Override
    public CommonResult queryPageRepairWorkTypes(int currentPage, int pageCount, String name) {
        PageUtils<RepairWorkType> result = repairWorkTypeService.queryPageRepairWorkTypes(currentPage, pageCount, name);
        return CommonResult.ok(result);
    }

    /**
     * 根据数据ID删除工种数据
     * @param id 数据ID
     * @return
     */
    @Override
    public CommonResult deleteRepairWorkTypeById(int id) {
        RepairWorkType data = repairWorkTypeService.getRepairById(id);
        if (data == null) {
            return CommonResult.fail("当前数据不存在，删除失败！");
        }

        int result = repairWorkTypeService.deleteRepairWorkTypeById(id);

        return result > 0 ? CommonResult.ok("删除成功") : CommonResult.fail("删除失败");
    }

    /**
     * 获取工种下拉列表数据
     * @return
     */
    @Override
    public CommonResult getRepairWorkTypes() {
        List<RepairWorkTypeVo> result = repairWorkTypeService.getRepairWorkTypes();

        return CommonResult.ok(result);
    }

    /**
     * 获取接单状态下拉数据
     * @return
     */
    @Override
    public CommonResult getReceivingState() {
        List<RepairWorkTypeVo> rs = new ArrayList<>();
        RepairWorkTypeVo r = new RepairWorkTypeVo();
        r.setId(1);
        r.setName("正常接单");
        rs.add(r);
        RepairWorkTypeVo r1 = new RepairWorkTypeVo();
        r1.setId(2);
        r1.setName("停止接单");
        rs.add(r1);
        return CommonResult.ok(rs);
    }

    /**
     * 身份列表数据查询
     * @return
     */
    @Override
    public CommonResult getRepairIdentitys() {
        List<RepairWorkTypeVo> result = new ArrayList<>();
        for(int i = 0;i<=4;i++){
            RepairWorkTypeVo data = new RepairWorkTypeVo();
            data.setId(i);
            data.setName(eUserZZ.stringOf(i));
            result.add(data);
        }
        return CommonResult.ok(result);
    }

}

