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

import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.util.ThreadPool;
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.service.DeptService;
import com.mortals.xhx.module.site.model.SiteEntity;
import com.mortals.xhx.module.site.model.SiteMatterEntity;
import com.mortals.xhx.module.site.model.SiteMatterQuery;
import com.mortals.xhx.module.site.model.SiteQuery;
import com.mortals.xhx.module.site.service.SiteMatterService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 站点事项
 *
 * @author zxfei
 * @date 2022-01-20
 */
@RestController
@RequestMapping("site/matter")
public class SiteMatterController extends BaseCRUDJsonBodyMappingController<SiteMatterService, SiteMatterEntity, Long> {

    @Autowired
    private ParamService paramService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private SiteService siteService;
    @Autowired
    private SiteMatterService siteMatterService;

    public SiteMatterController() {
        super.setModuleDesc("站点事项");
    }

    /**
     * @param query
     * @param model
     * @param context
     * @throws AppException
     */
    @Override
    protected void doListBefore(SiteMatterEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);
        query.setOrderColList(Arrays.asList(new OrderCol("hot", OrderCol.DESCENDING)));
        if (ObjectUtils.isEmpty(query.getEventTypeShowNotList())) {
            query.setEventTypeShowNotList(Arrays.asList("行政处罚"));
        }
    }


    /**
     * @param list
     * @param model
     * @param context
     * @return
     * @throws AppException
     */
    @Override
    protected int batchSaveAfter(List<SiteMatterEntity> list, Map<String, Object> model, Context context) throws AppException {
        //统计部门入驻数量
        Runnable runnable = new Runnable() {
            /**
             *
             */
            @Override
            public void run() {

                List<SiteEntity> siteEntities = siteService.find(new SiteQuery());
                for (SiteEntity siteEntity : siteEntities) {
                    DeptQuery query = new DeptQuery();
                    query.setSiteId(siteEntity.getId());
                    List<DeptEntity> deptEntities = deptService.find(query);
                    for (DeptEntity deptEntity : deptEntities) {
                        DeptEntity deptQuery = new DeptEntity();
                        deptQuery.setUpdateTime(new Date());

                        //统计入驻事项
                        SiteMatterQuery siteMatterQuery = new SiteMatterQuery();
                        siteMatterQuery.setDeptCode(deptEntity.getDeptNumber());
                        siteMatterQuery.setSource(SourceEnum.政务网.getValue());
                        siteMatterQuery.setHallCheckIn(YesNoEnum.YES.getValue());
                        int incount = siteMatterService.count(siteMatterQuery, null);
                        deptQuery.setInNum(incount);
                        DeptEntity condition = new DeptEntity();
                        condition.setId(deptEntity.getId());
                        deptService.getDao().update(deptQuery, condition);
                    }
                }
            }
        };
        ThreadPool.getInstance().execute(runnable);


        return super.batchSaveAfter(list, model, context);
    }
}