package com.gree.mall.manager.controller.zfire;

import cn.hutool.core.bean.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.AdminField;
import com.gree.mall.manager.plus.entity.AdminFieldCustomParam;
import com.gree.mall.manager.plus.entity.AdminFieldCustomParamItem;
import com.gree.mall.manager.plus.service.AdminFieldCustomParamItemService;
import com.gree.mall.manager.plus.service.AdminFieldCustomParamService;
import com.gree.mall.manager.plus.service.AdminFieldService;
import com.gree.mall.manager.zfire.bean.QueryParamBean;
import com.gree.mall.manager.zfire.bean.ZfireCustomParam;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("zfire")
public class ZfireController {

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    AdminFieldService adminFieldService;

    @Autowired
    AdminFieldCustomParamService adminFieldCustomParamService;

    @Autowired
    AdminFieldCustomParamItemService adminFieldCustomParamItemService;

    @PostMapping("save")
    @ApiOperation(value = "保存配置的列表字段")
    public ResponseHelper<List<AdminField>> save(@RequestBody List<AdminField> adminFieldList) {
        if (CollectionUtils.isEmpty(adminFieldList)) {
            return ResponseHelper.success();
        }
        String adminUserId = adminFieldList.get(0).getAdminUserId();
        String moduleId = adminFieldList.get(0).getModuleId();
        adminFieldService.lambdaUpdate().eq(AdminField::getAdminUserId, adminUserId).eq(AdminField::getModuleId, moduleId).remove();
        adminFieldService.saveBatch(adminFieldList);
        return ResponseHelper.success(adminFieldList);
    }

    @PostMapping("delete")
    @ApiOperation(value ="删除配置的列表字段")
    public ResponseHelper save(@RequestParam String adminUserId, @RequestParam String moduleId) {
        adminFieldService.lambdaUpdate().eq(AdminField::getAdminUserId, adminUserId).eq(AdminField::getModuleId, moduleId).remove();
        return ResponseHelper.success();
    }

    @PostMapping("param/save")
    @ApiOperation(value ="自定义的查询条件-保存")
    public ResponseHelper saveParam(@Valid @RequestBody ZfireCustomParam zfireCustomParam) {
        if (CollectionUtils.isEmpty(zfireCustomParam.getItems())) {
            throw new RemoteServiceException("缺少必要的参数items");
        }
        if (zfireCustomParam.getName().length() > 8) {
            throw new RemoteServiceException("自定义查询方案名称长度不可超过8");
        }
        AdminUserCom adminUser = commonLogic.getAdminUser();
        if (StringUtils.isBlank(zfireCustomParam.getId())) {
            Integer count = adminFieldCustomParamService.lambdaQuery().eq(AdminFieldCustomParam::getAdminUserId, adminUser.getAdminUserId())
                    .eq(AdminFieldCustomParam::getModuleId, zfireCustomParam.getModuleId()).count();
            if (count >= 10) {
                throw new RemoteServiceException("最多支持10个自定义查询方案");
            }
        }
        AdminFieldCustomParam adminFieldCustomParam = new AdminFieldCustomParam();
        adminFieldCustomParam.setId(zfireCustomParam.getId());
        adminFieldCustomParam.setName(zfireCustomParam.getName());
        adminFieldCustomParam.setModuleId(zfireCustomParam.getModuleId());
        adminFieldCustomParam.setAdminUserId(adminUser.getAdminUserId());
        adminFieldCustomParam.insertOrUpdate();
        adminFieldCustomParamItemService.lambdaUpdate().eq(AdminFieldCustomParamItem::getAdminFieldCustomId, adminFieldCustomParam.getId()).remove();
        List<AdminFieldCustomParamItem> items = BeanUtil.copyToList(zfireCustomParam.getItems(), AdminFieldCustomParamItem.class);
        items.forEach(v -> v.setAdminFieldCustomId(adminFieldCustomParam.getId()));
        adminFieldCustomParamItemService.saveBatch(items);
        return ResponseHelper.success();
    }

    @PostMapping("param/delete")
    @ApiOperation(value = "自定义的查询条件-删除")
    public ResponseHelper deleteParam(@RequestParam String id) {
        adminFieldCustomParamItemService.lambdaUpdate().eq(AdminFieldCustomParamItem::getAdminFieldCustomId, id).remove();
        adminFieldCustomParamService.removeById(id);
        return ResponseHelper.success();
    }

    @PostMapping("param/list")
    @ApiOperation(value = "自定义的查询条件-查询")
    public ResponseHelper<List<ZfireCustomParam>> listParam(@RequestParam String moduleId) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<AdminFieldCustomParam> list = adminFieldCustomParamService.lambdaQuery()
                .eq(AdminFieldCustomParam::getAdminUserId, adminUser.getAdminUserId())
                .eq(AdminFieldCustomParam::getModuleId, moduleId).list();
        List<ZfireCustomParam> zfireCustomBeans = BeanUtil.copyToList(list, ZfireCustomParam.class);
        for (ZfireCustomParam bean : zfireCustomBeans) {
            List<AdminFieldCustomParamItem> items = adminFieldCustomParamItemService.lambdaQuery().eq(AdminFieldCustomParamItem::getAdminFieldCustomId, bean.getId()).list();
            List<QueryParamBean> newItems = BeanUtil.copyToList(items, QueryParamBean.class);
            bean.setItems(newItems);
        }
        return ResponseHelper.success(zfireCustomBeans);
    }

    //    @ZfireList
    //    @PostMapping("list")
    //    @Operation(summary = "新方式重写MyMaterialController.list接口")
    //    public ResponseHelper<IPage<ZStockBean>> list(
    //            @RequestBody ZfireParamBean zfireParamBean
    //    ) {
    //        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
    //        IPage<ZStockBean> stockBeanIPage = myMaterialMapper.list2(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
    //        return ResponseHelper.success(stockBeanIPage, new TypeReference<ZStockBean>() {});
    //    }
    //
    //    @PostMapping("list/export")
    //    @Operation(summary = "新方式重写MyMaterialController.list接口的导出")
    //    public void listExport(
    //            @RequestBody ZfireParamBean zfireParamBean,
    //            HttpServletRequest request,
    //            HttpServletResponse response
    //    ) throws Exception {
    //        //1.组装查询条件
    //        ZfireParamBean zfireParam = FieldUtils.supplyParam(zfireParamBean);
    //        //2.查询要导出的内容
    //        IPage<ZStockBean> stockBeanIPage = myMaterialMapper.list2(new Page(zfireParam.getPageNum(), zfireParam.getPageSize()), zfireParam);
    //        //3.导出
    //        FieldUtils.exportData(stockBeanIPage.getRecords(),zfireParam.getExportFields(),request,response);
    //    }
}
