package cn.com.qmth.stmms.ms.admin.api;

import cn.com.qmth.stmms.ms.admin.service.OrganizationService;
import cn.com.qmth.stmms.ms.commons.utils.specification.PagingAndSortingDTO;
import cn.com.qmth.stmms.ms.core.domain.Organization;
import cn.com.qmth.stmms.ms.core.domain.user.MarkUser;
import cn.com.qmth.stmms.ms.core.domain.user.Role;
import cn.com.qmth.stmms.ms.core.repository.MarkUserRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 机构api
 */
@RestController
@RequestMapping("api/admin/organization")
public class OrganizationApi {

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private MarkUserRepo markUserRepo;

    /**
     * 机构管理列表
     *
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public PagingAndSortingDTO list(@RequestParam(required = false) String name,
                                    @RequestParam Integer curPage,
                                   @RequestParam Integer pageSize) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = new PageRequest(curPage, pageSize, sort);
        Specification<Organization> specification = (root, query, cb) -> {
            List<Predicate> predicates = new ArrayList<>();
            if (Objects.nonNull(name)) {
                predicates.add(cb.like(root.get("name"), "%" +name+ "%"));
            }

            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Page<Organization> organizations = organizationService.findAll(specification, pageable);
        List<Organization> organizations1 = organizations.getContent();
        organizations1.stream().map(o -> {
            List<MarkUser> markUsers = markUserRepo.findByOrganizationIdAndRole(o.getId(), Role.ADMIN);
            o.setMarkUsers(markUsers);
            return o;
        }).collect(Collectors.toList());
        return new PagingAndSortingDTO(organizations.getNumber(), organizations.getSize(), organizations.getTotalPages(),
                organizations.getTotalElements(), organizations1);
    }

    /**
     * 单个机构信息
     *
     * @param organization
     * @return
     */
    @RequestMapping(value = "{organization}", method = RequestMethod.GET)
    public Organization getOne(@PathVariable Organization organization) {
        return organization;
    }

    /**
     * 启用/禁用
     *
     * @param markUser
     * @return
     */
    @RequestMapping(value = "{markUser}", method = RequestMethod.POST)
    public MarkUser enabled(@PathVariable MarkUser markUser) {
        if (markUser != null) {
            markUser.setEnabled(!markUser.isEnabled());
        }
        return markUserRepo.save(markUser);
    }

    /**
     * 新增机构及管理员用户
     *
     * @param organization
     */
    @RequestMapping(method = RequestMethod.POST)
    public Organization add(@RequestBody Organization organization) {
        return organizationService.save(organization);
    }

    /**
     * 修改
     *
     * @param markUser
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public MarkUser updateOne(@RequestBody MarkUser markUser) {
        return organizationService.updateOne(markUser);
    }


    /**
     * 删除
     *
     * @param organizationId
     */
    @RequestMapping(value = "{organizationId}", method = RequestMethod.DELETE)
    public void remove(@PathVariable Long organizationId) {
        organizationService.delete(organizationId);
    }

}
