package com.tofly.feesapi.yhgl.controller_easy;

import com.tofly.feesapi.common.core.ResultRespone;
import com.tofly.feesapi.common.dbhelper.DbHelper;
import com.tofly.feesapi.common.util.DateUtils;
import com.tofly.feesapi.common.util.SqlConvertor;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/api/userprofilemgt/bwmeterzfb")
@Api(tags = "用户管理-总分表关系接口(easy)")
public class BwMeterZfbEasyController {

    @ApiOperation(value = "新增总分表关系(批量)")
    @PostMapping("/saveBatch")
    public ResultRespone saveBatch(@RequestBody Map<String, Object> data) throws Exception {
        //region 参数解析
        String yhbhZb = "";
        String yhbhFbList = "";
        String operatorCode = "";
        String operatorName = "";
        String operatorDatetime = "";
        try {
            yhbhZb = data.get("yhbhZb").toString();
            yhbhFbList = data.get("yhbhFbList").toString();
            if (data.get("czrybm") != null) operatorCode = data.get("czrybm").toString();
            if (data.get("czryxm") != null) operatorName = data.get("czryxm").toString();
            if (data.get("czsj") != null) {
                operatorDatetime = data.get("czsj").toString();
            } else {
                operatorDatetime = DateUtils.formatDate_pattern(new Date(), "yyyy-MM-dd HH:mm:ss");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultRespone.failed(e.getMessage());
        }
        //endregion

        //region 业务处理
        //验证
        if (StringUtils.isBlank(yhbhZb)) {
            return ResultRespone.failed("总表用户编号不能为空！");
        }
        if (StringUtils.isBlank(yhbhFbList)) {
            return ResultRespone.failed("分表用户编号数量不能为0！");
        }
        if (Arrays.asList(yhbhFbList.split(",")).contains(yhbhZb)) {
            return ResultRespone.failed("用户编号错误：选择的分表用户中包含了总表用户！");
        }
        //处理
        DbHelper db = DbHelper.getDbHelper();
        String strSql = MessageFormat.format("delete from yh_zfb where yhbh_fb in(''{0}'')", yhbhFbList.replace(",", "','"));
        db.addTransactionSql(strSql);
        strSql = "insert into yh_zfb select sys_guid(),yhbh_zb,yhbh_fb,cbkid_zb,cbkid_fb,'{2}','{3}',{4} from (\n" +
                "  select '1' k, yhbh as yhbh_zb,cbkid as cbkid_zb from yh_cbkxx where yhbh='{0}' and cbkh=1\n" +
                ") zb inner join (\n" +
                "  select '1' k, yhbh as yhbh_fb,cbkid as cbkid_fb from yh_cbkxx where yhbh in('{1}')\n" +
                ") fb on(zb.k=fb.k)";
        strSql = MessageFormat.format(strSql.replace("'", "''"), yhbhZb, yhbhFbList.replace(",", "','"), operatorCode, operatorName, SqlConvertor.toDate(operatorDatetime));
        db.addTransactionSql(strSql);

        boolean result = db.executeTransaction();
        if (!result) {
            return ResultRespone.failed("数据处理失败！");
        }

        return ResultRespone.success(result);
    }
}
