package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.system.domain.cost;
import com.ruoyi.system.domain.material;
import com.ruoyi.system.service.*;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController
{
    @Autowired
    private RedisCache redisCache;

    @Value(value = "${user.password.maxRetryCount}")
    private int maxRetryCount;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private TestService testService;

    /**
     * 获取用户列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUser user)
    {
        List<SysUser> list = userService.selectUserList(user);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.exportExcel(response, list, "用户数据");
    }

    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:user:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        List<SysUser> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = userService.importUser(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = { "/", "/{userId}" })
    public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
    {
        userService.checkUserDataScope(userId);
        AjaxResult ajax = AjaxResult.success();
        List<SysRole> roles = roleService.selectRoleAll();
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        ajax.put("posts", postService.selectPostAll());
        if (StringUtils.isNotNull(userId))
        {
            SysUser sysUser = userService.selectUserById(userId);
            ajax.put(AjaxResult.DATA_TAG, sysUser);
            ajax.put("postIds", postService.selectPostListByUserId(userId));
            ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
        }
        return ajax;
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysUser user)
    {
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        Pattern r = Pattern.compile(UserConstants.PASSWORD_REGEXP);
        Matcher m = r.matcher(user.getPassword());
        if(!m.matches()){
            return AjaxResult.error("密码格式错误，必须是6-20位包含字母和数字、特殊符号的组合");
        }
        user.setCreateBy(getUsername());
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysUser user)
    {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        if (!userService.checkUserNameUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        user.setUpdateBy(getUsername());
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public AjaxResult remove(@PathVariable Long[] userIds)
    {
        if (ArrayUtils.contains(userIds, getUserId()))
        {
            return error("当前用户不能删除");
        }
        return toAjax(userService.deleteUserByIds(userIds));
    }

    /**
     * 重置密码
     */
    @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public AjaxResult resetPwd(@RequestBody SysUser user)
    {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        Pattern r = Pattern.compile(UserConstants.PASSWORD_REGEXP);
        Matcher m = r.matcher(user.getPassword());
        if(!m.matches()){
            return AjaxResult.error("密码格式错误，必须是6-20位包含字母和数字、特殊符号的组合");
        }
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setUpdateBy(getUsername());
        return toAjax(userService.resetPwd(user));
    }

    /**
     * 账户解锁
     */
    @PreAuthorize("@ss.hasPermi('system:user:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @PutMapping("/unlock")
    public AjaxResult unlock(@RequestBody SysUser user)
    {
        Integer retryCount = redisCache.getCacheObject(getCacheKey(user.getUserName()));
        if (retryCount == null)
        {
            retryCount = 0;
        }
        if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
        {
            redisCache.deleteObject(getCacheKey(user.getUserName()));
        }

        return success("账户: "+user.getUserName()+" 解锁成功");
    }

    /**
     * 登录账户密码错误次数缓存键名
     *
     * @param username 用户名
     * @return 缓存键key
     */
    private String getCacheKey(String username)
    {
        return CacheConstants.PWD_ERR_CNT_KEY + username;
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysUser user)
    {
        userService.checkUserAllowed(user);
        userService.checkUserDataScope(user.getUserId());
        user.setUpdateBy(getUsername());
        return toAjax(userService.updateUserStatus(user));
    }

    /**
     * 根据用户编号获取授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping("/authRole/{userId}")
    public AjaxResult authRole(@PathVariable("userId") Long userId)
    {
        AjaxResult ajax = AjaxResult.success();
        SysUser user = userService.selectUserById(userId);
        List<SysRole> roles = roleService.selectRolesByUserId(userId);
        ajax.put("user", user);
        ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
        return ajax;
    }

    /**
     * 用户授权角色
     */
    @PreAuthorize("@ss.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PutMapping("/authRole")
    public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
    {
        userService.checkUserDataScope(userId);
        userService.insertUserAuth(userId, roleIds);
        return success();
    }

    /**
     * 获取部门树列表
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }

    /*===============================================测试====================================================*/
    /*@DataSource(DataSourceType.QUOT)
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        List<temp> templist = testService.selectTemp();
        List<material> materials = new ArrayList<material>();
        material m = new material();

        List<cost> costs = new ArrayList<cost>();
        cost c = new cost();

        List<List<temp>> averageAssign =  ListUtils.partition(templist, 5000);


        int num = 1;
        int num2 = 1;
        for(List<temp> list:averageAssign){
            for(temp t:list){
                m = new material();
                m.setMaterial_id(num);
                m.setMaterial_xingh(t.getModel_1());
                m.setMaterial_guig(t.getSpec_1());
                m.setMaterial_diany(t.getVoltage_1());
                m.setMaterial_dw(t.getUnit_0());
                m.setMaterial_type_id(t.getType_no());
                materials.add(m);
                num++;

                if("M001".equals(t.getM_id_M001())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M001());
                    c.setCost_cl_qty(t.getName_qty_M001());
                    costs.add(c);num2++;
                }
                if("M002".equals(t.getM_id_M002())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M002());
                    c.setCost_cl_qty(t.getName_qty_M002());
                    costs.add(c);num2++;
                }
                if("M003".equals(t.getM_id_M003())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M003());
                    c.setCost_cl_qty_2(t.getName_qty_M003_1());
                    costs.add(c);num2++;
                }
                if("M004".equals(t.getM_id_M004())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M004());
                    c.setCost_cl_qty(t.getName_qty_M004());
                    c.setCost_cl_qty_2(t.getName_qty_M004_1());
                    costs.add(c);num2++;
                }
                if("M005".equals(t.getM_id_M005())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M005());
                    c.setCost_cl_qty(t.getName_qty_M005());
                    c.setCost_cl_qty_2(t.getName_qty_M005_1());
                    costs.add(c);num2++;
                }
                if("M006".equals(t.getM_id_M006())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M006());
                    c.setCost_cl_qty(t.getName_qty_M006());
                    costs.add(c);num2++;
                }
                if("M007".equals(t.getM_id_M007())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M007());
                    c.setCost_cl_qty(t.getName_qty_M007());
                    costs.add(c);num2++;
                }
                if("M008".equals(t.getM_id_M008())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M008());
                    c.setCost_cl_qty(t.getName_qty_M008());
                    costs.add(c);num2++;
                }
                if("M009".equals(t.getM_id_M009())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M009());
                    c.setCost_cl_qty(t.getName_qty_M009());
                    costs.add(c);num2++;
                }
                if("M010".equals(t.getM_id_M010())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M010());
                    c.setCost_cl_qty(t.getName_qty_M010());
                    c.setCost_cl_qty_2(t.getName_qty_M010_1());
                    costs.add(c);num2++;
                }
                if("M011".equals(t.getM_id_M011())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M011());
                    c.setCost_cl_qty(t.getName_qty_M011());
                    costs.add(c);num2++;
                }
                if("M012".equals(t.getM_id_M012())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M012());
                    c.setCost_cl_qty(t.getName_qty_M012());
                    costs.add(c);num2++;
                }
                if("M013".equals(t.getM_id_M013())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M013());
                    c.setCost_cl_qty(t.getName_qty_M013());
                    costs.add(c);num2++;
                }if("M014".equals(t.getM_id_M014())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M014());
                    c.setCost_cl_qty(t.getName_qty_M014());
                    costs.add(c);num2++;
                }if("M015".equals(t.getM_id_M015())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M015());
                    c.setCost_cl_qty(t.getName_qty_M015());
                    costs.add(c);num2++;
                }
                if("M016".equals(t.getM_id_M016())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M016());
                    c.setCost_cl_qty(t.getName_qty_M016());
                    costs.add(c);num2++;
                }
                if("M017".equals(t.getM_id_M017())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M017());
                    c.setCost_cl_qty(t.getName_qty_M017());
                    costs.add(c);num2++;
                }
                if("M018".equals(t.getM_id_M018())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M018());
                    c.setCost_cl_qty(t.getName_qty_M018());
                    costs.add(c);num2++;
                }if("M019".equals(t.getM_id_M019())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M019());
                    c.setCost_cl_qty(t.getName_qty_M019());
                    c.setCost_cl_qty_2(t.getName_qty_M019_1());
                    costs.add(c);num2++;
                }
                if("M020".equals(t.getM_id_M020())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M020());
                    c.setCost_cl_qty(t.getName_qty_M020());
                    costs.add(c);num2++;
                }
                if("M021".equals(t.getM_id_M021())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M021());
                    c.setCost_cl_qty(t.getName_qty_M021());
                    c.setCost_cl_qty_2(t.getName_qty_M021_1());
                    costs.add(c);num2++;
                }
                if("M022".equals(t.getM_id_M022())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M022());
                    c.setCost_cl_qty(t.getName_qty_M022());
                    costs.add(c);num2++;
                }
                if("M023".equals(t.getM_id_M023())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M023());
                    c.setCost_cl_qty(t.getName_qty_M023());
                    costs.add(c);num2++;
                }if("M024".equals(t.getM_id_M024())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M024());
                    c.setCost_cl_qty(t.getName_qty_M024());
                    costs.add(c);num2++;
                }if("M025".equals(t.getM_id_M025())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M025());
                    c.setCost_cl_qty(t.getName_qty_M025());
                    costs.add(c);num2++;
                }
                if("M026".equals(t.getM_id_M026())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M026());
                    c.setCost_cl_qty(t.getName_qty_M026());
                    costs.add(c);num2++;
                }
                if("M027".equals(t.getM_id_M027())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M027());
                    c.setCost_cl_qty(t.getName_qty_M027());
                    costs.add(c);num2++;
                }
                if("M028".equals(t.getM_id_M028())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M028());
                    c.setCost_cl_qty(t.getName_qty_M028());
                    costs.add(c);num2++;
                }
                if("M029".equals(t.getM_id_M029())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M029());
                    c.setCost_cl_qty(t.getName_qty_M029());
                    costs.add(c);num2++;
                }if("M030".equals(t.getM_id_M030())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M030());
                    c.setCost_cl_qty(t.getName_qty_M030());
                    costs.add(c);num2++;
                }if("M031".equals(t.getM_id_M031())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M031());
                    c.setCost_cl_qty(t.getName_qty_M031());
                    costs.add(c);num2++;
                }if("M032".equals(t.getM_id_M032())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M032());
                    c.setCost_cl_qty(t.getName_qty_M032());
                    costs.add(c);num2++;
                }if("M033".equals(t.getM_id_M033())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M033());
                    c.setCost_cl_qty(t.getName_qty_M033());
                    costs.add(c);num2++;
                }if("M034".equals(t.getM_id_M034())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M034());
                    c.setCost_cl_qty(t.getName_qty_M034());
                    costs.add(c);num2++;
                }
                if("M035".equals(t.getM_id_M035())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M035());
                    c.setCost_cl_qty(t.getName_qty_M035());
                    costs.add(c);num2++;
                }if("M036".equals(t.getM_id_M036())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M036());
                    c.setCost_cl_qty(t.getName_qty_M036());
                    costs.add(c);num2++;
                }if("M037".equals(t.getM_id_M037())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M037());
                    c.setCost_cl_qty(t.getName_qty_M037());
                    c.setCost_cl_qty_2(t.getName_qty_M037_1());
                    costs.add(c);num2++;
                }
                if("M038".equals(t.getM_id_M038())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M038());
                    c.setCost_cl_qty(t.getName_qty_M038());
                    costs.add(c);num2++;
                }
                if("M039".equals(t.getM_id_M039())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M039());
                    c.setCost_cl_qty(t.getName_qty_M039());
                    c.setCost_cl_qty_2(t.getName_qty_M039_1());
                    costs.add(c);num2++;
                }
                if("M040".equals(t.getM_id_M040())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M040());
                    c.setCost_cl_qty(t.getName_qty_M040());
                    costs.add(c);num2++;
                }
                if("M041".equals(t.getM_id_M041())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M041());
                    c.setCost_cl_qty(t.getName_qty_M041());
                    costs.add(c);num2++;
                }
                if("M042".equals(t.getM_id_M042())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M042());
                    c.setCost_cl_qty(t.getName_qty_M042());
                    c.setCost_cl_qty_2(t.getName_qty_M042_1());
                    costs.add(c);num2++;
                }
                if("M043".equals(t.getM_id_M043())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M043());
                    c.setCost_cl_qty(t.getName_qty_M043());
                    costs.add(c);num2++;
                }
                if("M044".equals(t.getM_id_M044())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M044());
                    c.setCost_cl_qty(t.getName_qty_M044());
                    costs.add(c);num2++;
                }
                if("M045".equals(t.getM_id_M045())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M045());
                    c.setCost_cl_qty(t.getName_qty_M045());
                    costs.add(c);num2++;
                }
                if("M046".equals(t.getM_id_M046())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M046());
                    c.setCost_cl_qty(t.getName_qty_M046());
                    costs.add(c);num2++;
                }if("M047".equals(t.getM_id_M047())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M047());
                    c.setCost_cl_qty(t.getName_qty_M047());
                    costs.add(c);num2++;
                }if("M048".equals(t.getM_id_M048())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M048());
                    c.setCost_cl_qty(t.getName_qty_M048());
                    c.setCost_cl_qty_2(t.getName_qty_M048_1());
                    costs.add(c);num2++;
                }if("M049".equals(t.getM_id_M049())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M049());
                    c.setCost_cl_qty(t.getName_qty_M049());
                    costs.add(c);num2++;
                }
                if("M050".equals(t.getM_id_M050())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M050());
                    c.setCost_cl_qty(t.getName_qty_M050());
                    costs.add(c);num2++;
                }
                if("M051".equals(t.getM_id_M051())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M051());
                    c.setCost_cl_qty(t.getName_qty_M051());
                    costs.add(c);num2++;
                }
                if("M052".equals(t.getM_id_M052())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M052());
                    c.setCost_cl_qty(t.getName_qty_M052());
                    costs.add(c);num2++;
                }
                if("M053".equals(t.getM_id_M053())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M053());
                    c.setCost_cl_qty(t.getName_qty_M053());
                    costs.add(c);num2++;
                }
                if("M054".equals(t.getM_id_M054())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M054());
                    c.setCost_cl_qty(t.getName_qty_M054());
                    costs.add(c);num2++;
                }if("M055".equals(t.getM_id_M055())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M055());
                    c.setCost_cl_qty(t.getName_qty_M055());
                    costs.add(c);num2++;
                }if("M056".equals(t.getM_id_M056())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M056());
                    c.setCost_cl_qty(t.getName_qty_M056());
                    costs.add(c);num2++;
                }if("M057".equals(t.getM_id_M057())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M057());
                    c.setCost_cl_qty(t.getName_qty_M057());
                    costs.add(c);num2++;
                }if("M058".equals(t.getM_id_M058())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M058());
                    c.setCost_cl_qty(t.getName_qty_M058());
                    costs.add(c);num2++;
                }
                if("M059".equals(t.getM_id_M059())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M059());
                    c.setCost_cl_qty(t.getName_qty_M059());
                    costs.add(c);num2++;
                }
                if("M060".equals(t.getM_id_M060())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M060());
                    c.setCost_cl_qty(t.getName_qty_M060());
                    costs.add(c);num2++;
                }
                if("M061".equals(t.getM_id_M061())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M061());
                    c.setCost_cl_qty(t.getName_qty_M061());
                    costs.add(c);num2++;
                }
                if("M062".equals(t.getM_id_M062())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M062());
                    c.setCost_cl_qty(t.getName_qty_M062());
                    costs.add(c);num2++;
                }
                if("M063".equals(t.getM_id_M063())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M063());
                    c.setCost_cl_qty(t.getName_qty_M063());
                    costs.add(c);num2++;
                }
                if("M064".equals(t.getM_id_M064())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M064());
                    c.setCost_cl_qty(t.getName_qty_M064());
                    costs.add(c);num2++;
                }
                if("M065".equals(t.getM_id_M065())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M065());
                    c.setCost_cl_qty(t.getName_qty_M065());
                    costs.add(c);num2++;
                }
                if("M066".equals(t.getM_id_M066())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M066());
                    c.setCost_cl_qty(t.getName_qty_M066());
                    costs.add(c);num2++;
                }
                if("M067".equals(t.getM_id_M067())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M067());
                    c.setCost_cl_qty(t.getName_qty_M067());
                    costs.add(c);num2++;
                }
                if("M068".equals(t.getM_id_M068())){
                    c = new cost();
                    c.setCost_id(num2);
                    c.setCost_material_id(m.getMaterial_id());
                    c.setCost_cl_id(t.getM_id_M068());
                    c.setCost_cl_qty(t.getName_qty_M068());
                    costs.add(c);num2++;
                }
            }
        }


        System.out.println("materials===================="+materials.size());
        System.out.println("costs======================="+costs.size());

        batchInsert.insertMaterialBatch(materials);
        batchInsert.insertCostBatch(costs);
    }*/

    @DataSource(DataSourceType.QUOT)
    public void insertMaterialBatch(List<material> temp) {
        testService.insertMaterialBatch(temp);
    }

    @DataSource(DataSourceType.QUOT)
    public void insertCostBatch(List<cost> temp) {
        testService.insertCostBatch(temp);
    }
}
