package com.huyi.service.common;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huyi.service.base.entity.PubColumnSettingsInf;
import com.huyi.service.base.service.IPubColumnSettingsInfService;
import com.tianhu.common.core.domain.R;
import com.tianhu.common.core.utils.CommonUtil;
import com.tianhu.common.core.web.controller.BaseController;
import com.tianhu.common.core.web.domain.AjaxResult;
import com.tianhu.common.log.annotation.Log;
import com.tianhu.common.log.enums.BusinessType;
import com.tianhu.common.security.service.TokenService;
import com.tianhu.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 个性设置列Controller
 * 
 * @author tianhu
 * @date 2021-02-04
 */
@RestController
@RequestMapping("/ownColumnsetting")
public class OwnColumnsettingController extends BaseController
{
    //自动生成的列设置类
    @Autowired
    private IPubColumnSettingsInfService columnSettingsInfService;
    //token
    @Autowired
    private TokenService tokenService;


    /**
     * 配置列显示隐藏
     * @param map
     * @return
     * @throws ParseException
     */
    @Log(title = "列设置管理", businessType = BusinessType.INSERT)
    @Transactional(rollbackFor=Exception.class)
    @PostMapping
    public AjaxResult add(@RequestBody Map<String,Object> map) throws ParseException {
        //获取用户信息
        LoginUser userInfo = tokenService.getLoginUser();
        //用户id
        String userId = CommonUtil.objToString(userInfo.getUserid());
        //企业id
        String companyId = userInfo.getSysUser().getCompanyId();
        //获取页面路径
        String psfPagePath = CommonUtil.objToString(map.get("psfPagePath"));
        if(CommonUtil.isEmpty(psfPagePath)){
            return AjaxResult.error("页面路径不能为空");
        }
        //获取页面表格名称
        String psfTableName = CommonUtil.objToString(map.get("psfTableName"));
        if(CommonUtil.isEmpty(psfTableName)){
            return AjaxResult.error("表格名称不能为空");
        }
        //获取显示的列
        List<String> psfShowDataList = (List<String>)map.get("psfShowData");
        if(psfShowDataList.size() < 1){
            return AjaxResult.error("配置显示列不能为空");
        }
        String psfShowData = "[" + String.join(",", psfShowDataList) + "]";

        //先查询是否存在
        LambdaQueryWrapper<PubColumnSettingsInf> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfCompanyId,companyId);
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfUserId,userId);
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfPagePath,psfPagePath);
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfTableName,psfTableName);
        List<PubColumnSettingsInf>  list  = columnSettingsInfService.findPubColumnSettingsInfs(lambdaQueryWrapper);
        PubColumnSettingsInf columnSettingsInf = new PubColumnSettingsInf();
        //公司
        columnSettingsInf.setPsfCompanyId(companyId);
        //用户编号
        columnSettingsInf.setPsfUserId(userId);
        columnSettingsInf.setPsfPagePath(psfPagePath);
        columnSettingsInf.setPsfTableName(psfTableName);
        columnSettingsInf.setPsfShowData(psfShowData);
        //没有，执行新增
        if(list.size() < 1){
            columnSettingsInfService.createPubColumnSettingsInf(columnSettingsInf);
        }else{
            //执行修改
            columnSettingsInfService.update(columnSettingsInf,lambdaQueryWrapper);
        }
        return AjaxResult.success();
    }

    /**
     * 查询当前用户配置的列隐藏与显示
     * @param map
     * @return
     */
    @GetMapping("/list")
    public R list(@RequestParam(required = false) Map map) {
        //获取用户信息
        LoginUser userInfo = tokenService.getLoginUser();
        //企业id
        String companyId = userInfo.getSysUser().getCompanyId();
        //用户id
        String userId = CommonUtil.objToString(userInfo.getUserid());
        //获取页面路径
        String psfPagePath = CommonUtil.objToString(map.get("psfPagePath"));
        if(CommonUtil.isEmpty(psfPagePath)){
            return R.ok("页面路径不能为空");
        }
        //获取页面表格名称
        String psfTableName = CommonUtil.objToString(map.get("psfTableName"));
        if(CommonUtil.isEmpty(psfTableName)){
            return R.ok("表格名称不能为空");
        }
        LambdaQueryWrapper<PubColumnSettingsInf> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfCompanyId,companyId);
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfUserId,userId);
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfPagePath,psfPagePath);
        lambdaQueryWrapper.eq(PubColumnSettingsInf::getPsfTableName,psfTableName);
        List<PubColumnSettingsInf>  list  = columnSettingsInfService.findPubColumnSettingsInfs(lambdaQueryWrapper);
        if (list.size() > 0){
            PubColumnSettingsInf pubColumnSettingsInf = list.get(0);
            Map<String,Object> p  = CommonUtil.entityToMap(pubColumnSettingsInf);
            //获取显示列
           String  showData   = CommonUtil.objToString(p.get("psfShowData"));
           System.out.println(showData);
           //不为空
           if(!CommonUtil.isEmpty((showData))){
               showData = showData.replace("[", "").replace("]", "");
               String[] strs = showData.split(",");
               List<String> showDataList = new ArrayList<String>();
               for (int i = 0; i < strs.length; i++) {
                   showDataList.add(strs[i]);
               }
               p.put("psfShowData",showDataList);
           }
            return R.ok(p);
        }else{
            Map m  = new HashMap();
            return R.ok(m);
        }

    }




}
