package cn.com.ty.lift.enterprise.option.controller;


import cn.com.ty.lift.enterprise.option.dao.entity.MaintenanceOption;
import cn.com.ty.lift.enterprise.option.dao.entity.model.OptionReq;
import cn.com.ty.lift.enterprise.option.service.impl.MaintenanceOptionServiceImpl;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
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 java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 保养项目 前端控制器
 * </p>
 *
 * @author wang-hai-cheng
 * @since 2019-12-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/maintenance-option")
public class MaintenanceOptionController {
    private final MaintenanceOptionServiceImpl optionService;

    /**
     * 分页获取维保项列表
     *
     * @param req 页码 页大小
     * @return cn.com.xwy.boot.web.dto.RestResponse
     */
    @PostMapping("/list")
    public RestResponse list(@RequestBody OptionReq req) {
        IPage<MaintenanceOption> page = optionService.page(
                new Page<>(req.getPageNum(), req.getPageSize()), new QueryWrapper<MaintenanceOption>()
                        //10086L是固定数据的公司id
                        .in("mt_company_id", 10086L, req.getMtCompanyId())
                        .in(req.getType() != null, "type", type(req))
                        .eq(req.getLiftCategory() != null, "lift_category", req.getLiftCategory())
                        .eq("status", 2)
                        .orderByAsc("id"));
        if (page.getRecords().isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(page);
    }

    /**
     * app端维保项列表
     *
     * @param req
     * @return RestResponse
     */
    @PostMapping("/options")
    public RestResponse options(@RequestBody OptionReq req) {
        Integer category = req.getLiftCategory();
        String sort = null;
        if (category == 4 || category == 5) {
            req.setLiftCategory(3);
            sort = "-1";
        }
        if (category == 3) {
            req.setLiftCategory(4);
        }
        List<MaintenanceOption> options = optionService.list(
                new QueryWrapper<MaintenanceOption>()
                        //10086L是固定数据的公司id
                        .in("mt_company_id", 10086L, req.getMtCompanyId())
                        .in(req.getType() != null, "type", type(req))
                        .eq(req.getLiftCategory() != null, "lift_category", req.getLiftCategory())
                        .eq("status", 2)
                        .in(sort != null, "sort", sort));
        if (options.isEmpty()) {
            return RestResponse.success();
        }
        return RestResponse.success(options);
    }

    /**
     * 新增维保项
     *
     * @param option 维保项信息
     * @return RestResponse
     */
    @PostMapping("/add")
    public RestResponse add(@RequestBody MaintenanceOption option) {
        if (option.getLiftCategory() == 3) {
            option.setSort(-1);
        }
        //当前维保项版本是2
        option.setStatus(2);
        return RestResponse.success(optionService.save(option));
    }

    /**
     * 判断是否默认维保项，不是默认维保项才可以更新
     *
     * @param option 维保项信息
     * @return RestResponse
     */
    @PostMapping("/modify")
    public RestResponse modify(@RequestBody MaintenanceOption option) {
        Long mtCompanyId = optionService.getById(option.getId()).getMtCompanyId();
        //10086L是固定数据的公司id
        if (option.getMtCompanyId().equals(mtCompanyId) || option.getMtCompanyId().equals(10086L)) {
            return RestResponse.success(optionService.updateById(option));
        }
        return RestResponse.fail("默认维保项无法编辑");
    }

    /**
     * 判断是否是默认维保项，不是默认的维保项才可以删除
     *
     * @param option 维保项id
     * @return RestResponse
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody MaintenanceOption option) {
        Long mtCompanyId = optionService.getById(option.getId()).getMtCompanyId();
        //10086L是固定数据的公司id
        if (option.getMtCompanyId().equals(mtCompanyId) || option.getMtCompanyId().equals(10086L)) {
            return RestResponse.success(optionService.removeById(option.getId()));
        }
        return RestResponse.fail("默认维保项无法删除");
    }

    /**
     * 维保项type 1半月 2季度保 3半年保 4全年保(仅仅只有自己的，
     * 比如type=2时，并没有1的维保项，所以要处理一下，把1包含进去
     *
     * @param req
     * @return type列表
     */
    private ArrayList<Integer> type(OptionReq req) {
        ArrayList<Integer> types = new ArrayList<>();
        Integer type = req.getType();
        if (type > 0)
            types.add(1);
        if (type > 1)
            types.add(2);
        if (type > 2)
            types.add(3);
        if (type > 3)
            types.add(4);
        return types;
    }
}