package com.mortals.xhx.module.dept.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.common.code.SourceEnum;
import com.mortals.xhx.common.code.YesNoEnum;
import com.mortals.xhx.module.dept.model.DeptEntity;
import com.mortals.xhx.module.dept.model.DeptQuery;
import com.mortals.xhx.module.dept.model.vo.DeptVo;
import com.mortals.xhx.module.dept.service.DeptService;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.service.SiteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门
 *
 * @author zxfei
 * @date 2022-01-20
 */
@RestController
@RequestMapping("dept")
public class DeptController extends BaseCRUDJsonBodyMappingController<DeptService, DeptEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private SiteService siteService;

    public DeptController() {
        super.setModuleDesc("部门");
    }

    @Override
    protected void doListBefore(DeptEntity query, Map<String, Object> model, Context context) throws AppException {

        if (ObjectUtils.isEmpty(query.getIdList())) {
            if (ObjectUtils.isEmpty(query.getOrderColList())) {
                query.setOrderColList(new ArrayList<OrderCol>() {
                    {
                        add(new OrderCol("sort", OrderCol.ASCENDING));
                        add(new OrderCol("createTime", OrderCol.DESCENDING));
                    }
                });
            } else {
                query.getOrderColList().add(new OrderCol("createTime", OrderCol.DESCENDING));
            }
        } else {

        }
        if (!ObjectUtils.isEmpty(query.getFilter()) && YesNoEnum.YES.getValue() == query.getFilter()) {
            //过滤部门事项数据为0的部门
            query.setTotalStart(1);
        }

        if (!ObjectUtils.isEmpty(query.getHallCheckIn()) && YesNoEnum.YES.getValue() == query.getHallCheckIn()) {
            //过滤部门事项数据为0的部门
            query.setInNumStart(1);
        }
        super.doListBefore(query, model, context);
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "isAutotable", paramService.getParamBySecondOrganize("Dept", "isAutotable"));
        this.addDict(model, "isOrder", paramService.getParamBySecondOrganize("Dept", "isOrder"));
        this.addDict(model, "isBkb", paramService.getParamBySecondOrganize("Dept", "isBkb"));
        this.addDict(model, "isWorkGuide", paramService.getParamBySecondOrganize("Dept", "isWorkGuide"));
        this.addDict(model, "usValid", paramService.getParamBySecondOrganize("Dept", "usValid"));
        this.addDict(model, "isSecphone", paramService.getParamBySecondOrganize("Dept", "isSecphone"));
        this.addDict(model, "isEnglish", paramService.getParamBySecondOrganize("Dept", "isEnglish"));
        super.init(model, context);
    }


    /**
     * @param entity
     * @param model
     * @param context
     * @throws AppException
     */
    @Override
    protected void saveBefore(DeptEntity entity, Map<String, Object> model, Context context) throws AppException {
        if (entity.newEntity()) {
            entity.setSource(SourceEnum.自定义.getValue());

        }
        super.saveBefore(entity, model, context);
    }
}