package com.zhiqiyun.open.router.apis;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dliyun.oap.framework.annotation.ServiceMethod;
import com.dliyun.oap.framework.annotation.ServiceMethodBean;
import com.dliyun.oap.framework.response.OapResponse;
import com.zhiqiyun.open.core.models.category.CategoryMaterial;
import com.zhiqiyun.open.core.models.category.CategoryOccupation;
import com.zhiqiyun.open.core.models.category.CategoryOrganization;
import com.zhiqiyun.open.core.service.CategoryMaterialService;
import com.zhiqiyun.open.core.service.CategoryOccupationService;
import com.zhiqiyun.open.core.service.CategoryOrganizationService;
import com.zhiqiyun.open.core.service.SequenceService;
import com.zhiqiyun.open.router.request.category.*;
import com.zhiqiyun.open.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
@ServiceMethodBean
public class CategoryApi {

    @Autowired
    private CategoryMaterialService categoryMaterialService;

    @Autowired
    private CategoryOccupationService categoryOccupationService;

    @Autowired
    private CategoryOrganizationService categoryOrganizationService;

    @Autowired
    private SequenceService sequenceService;

    @ServiceMethod(method = "list.category.occupation", title = "查询文旅岗位目录")
    public OapResponse listCategoryOccupation(CategoryOccupationRequest request) {
        QueryWrapper<CategoryOccupation> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(request.getName())) {
            queryWrapper.like("name", request.getName());
        }
        if (request.getId() != null) {
            queryWrapper.eq("id", request.getId());
        }

        queryWrapper.orderByDesc("sort_number");
        queryWrapper.orderByDesc("id");

        List<CategoryOccupation> listData = this.categoryOccupationService.list(queryWrapper);

        return OapResponse.success().setBody(listData);
    }

    @ServiceMethod(method = "list.category.occupation.page", title = "查询文旅岗位分页目录")
    public OapResponse listCategoryOccupationPage(CategoryMaterialPageRequest request) {
        QueryWrapper<CategoryOccupation> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(request.getName())) {
            wrapper.like("name", request.getName());
        }
        if (request.getId() != null) {
            wrapper.eq("id", request.getId());
        }
        Page<CategoryOccupation> page = new Page<>(request.getCurrent(), request.getPageSize());
        page.addOrder(OrderItem.asc("sort_number"));
        page.addOrder(OrderItem.desc("id"));
        Page<CategoryOccupation> resultData = this.categoryOccupationService.page(page, wrapper);
        return OapResponse.success().setBody(resultData);
    }

    @ServiceMethod(method = "list.category.material", title = "物务资源目录")
    public OapResponse listPageSceneryInfo(CategoryMaterialRequest request) {
        QueryWrapper<CategoryMaterial> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(request.getName())) {
            queryWrapper.like("name", request.getName());
        }
        if (request.getId() != null) {
            queryWrapper.eq("id", request.getId());
        }
        queryWrapper.orderByDesc("sort_number");
        queryWrapper.orderByDesc("id");
        List<CategoryMaterial> listData = this.categoryMaterialService.list(queryWrapper);
        return OapResponse.success().setBody(listData);
    }

    @ServiceMethod(method = "list.category.material.page", title = "物务资源分页目录")
    public OapResponse listPageMaterialPage(CategoryMaterialPageRequest request) {
        QueryWrapper<CategoryMaterial> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(request.getName())) {
            wrapper.like("name", request.getName());
        }
        if (request.getId() != null) {
            wrapper.eq("id", request.getId());
        }
        Page<CategoryMaterial> page = new Page<>(request.getCurrent(), request.getPageSize());
        page.addOrder(OrderItem.asc("sort_number"));
        page.addOrder(OrderItem.desc("id"));
        Page<CategoryMaterial> resultData = this.categoryMaterialService.page(page, wrapper);
        return OapResponse.success().setBody(resultData);
    }

    @ServiceMethod(method = "list.category.organization", title = "机构列表")
    public OapResponse listPageOrganization(CategoryOrganizationRequest request) {
        QueryWrapper<CategoryOrganization> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotBlank(request.getName())) {
            queryWrapper.like("name", request.getName());
        }
        queryWrapper.orderByDesc("id");
        List<CategoryOrganization> listData = this.categoryOrganizationService.list(queryWrapper);
        return OapResponse.success().setBody(listData);
    }

    @ServiceMethod(method = "list.category.organization.save", title = "机构保存")
    public OapResponse listPageOrganizationSave(CategoryOrganizationSaveRequest request) {
        CategoryOrganization categoryOrganization;
        if (request.getId() == null) {
            QueryWrapper<CategoryOrganization> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", request.getName()).last("limit 1");
            categoryOrganization = this.categoryOrganizationService.getOne(queryWrapper);
            if (categoryOrganization != null) {
                return OapResponse.success().setBody(categoryOrganization);
            }
        }
        categoryOrganization = new CategoryOrganization();
        categoryOrganization.setName(request.getName());
        categoryOrganization.setSocialCode(request.getSocialCode());
        categoryOrganization.setBusinessLicense(request.getBusinessLicense());
        categoryOrganization.setLegalPersonName(request.getLegalPersonName());
        categoryOrganization.setLegalPersonIdCardA(request.getLegalPersonIdCardA());
        categoryOrganization.setLegalPersonIdCardB(request.getLegalPersonIdCardB());
        categoryOrganization.setContacts(request.getContacts());
        categoryOrganization.setContactNumber(request.getContactNumber());
        categoryOrganization.setContactAddress(request.getContactAddress());
        categoryOrganization.setRemark(request.getRemark());
        categoryOrganization.setLat(request.getLat());
        categoryOrganization.setLng(request.getLng());
        if (request.getId() == null) {
            categoryOrganization.setId(this.sequenceService.nextId());
            categoryOrganization.setCreatedTime(DateUtil.current());
            this.categoryOrganizationService.save(categoryOrganization);
        } else {
            categoryOrganization.setId(request.getId());
            this.categoryOrganizationService.updateById(categoryOrganization);
        }
        return OapResponse.success().setBody(categoryOrganization);
    }

}
